Skip to content
TORNLIFE More

Dynamic key in Bazaar API

Started by Stewie [1494547] on in API Development.

38 replies · 534 views · thread synced · 7 days ago · View on torn.com

Posts archived: 39 / 39 posts (100%) · the total is Torn's reply count + the opening post at the last fetch

Plornt [1799359]

Not really?

JSON specifies 7 primative values: string, number, object (/map), array, true, false, null

Im not understanding what you are saying because even if so, even if "by default json was a map" then what does that matter from a logical standpoint? Should we define numbers in object format too in that case?
Stewie [1494547]

I'm not really arguing about the structure of the API. The structure is only a small part of it.

Here is my problem: {"bazaar":{"1234567":{

That value is hard to reference to, (at least, it seems to be within mIRC (mSL))

It is for the most part, poorly structured like this (as well as a lot of other places in the API.)

There is no key for that value though.. well, that is the "key", but I cannot reference that.

"cost":24500, has the key "cost" and the value "24500"

I am by no means arguing that the number cannot be dynamic, as, yes, it should be.
IceBlueFire [776] Officer Officer

It's the listing ID that has absolutely no relevance to anything within the game. It's purely there for the system to track which listing it is. I'm not really sure what the issue is or why you care to track it...
Stewie [1494547]

I do not care to track that number, it's not that number I need... but, with the parser I use I would do $JSON(<handle>,bazaar,<number>,cost). I cannot reference cost without that number.
Mauk [1494436]

That's exactly the reason it shouldn't be used as a map key.

--

"The format itself is considered a map that maps keys to values."

Yeah, it actually isn't. "null" is a completely valid JSON, and so is "54" or "[12,25]." They really, really aren't maps. Please, refer to the JSON standard if you still don't believe that.
IceBlueFire [776] Officer Officer

We needed a unique value to identify each listing. Figured the best way to keep track of them (if you wanted to) was to use the listing ID, rather than just numbering them.
Plornt [1799359]

I think people are getting confused. We're not saying it should not be there, its more on the lines of it should not be used as the key portion of a key value map, it doesnt make sense. Your bazaar item list is a list. The API should return a list of items in your bazaar rather than a map that uses the ID as keys,.
Plornt [1799359]

Not only is that wrong its also completely goddamn irrelevant regardless. No one is saying it couldnt be { "bazaar": [ Actual list/array of items ] } in fact that is what Stewies first post states as an example.

Edit: Didn't mean that to come off as harsh...
Plornt [1799359]

Can you repeat what your actual point is? I'm completely missing it currently reading back through your posts. It seems you are arguing something irrelevant to what this topic is actually about.

Yes all Torns API calls provide a map, but the value of the bazaar property/field should be a list of items. It doesnt make sense that the value is a object/map of bazaar row id -> amount.
archimede [1565089]

I'm afraid you have to give up.

For some reason, someone decided that arrays inside JSON are evil/complicated/unnecessary/whatever: I already tried to suggest this isn't a good idea (eee here) to no avail.

You'll have to live with the current state of affairs.

Happy coding.

Mentions: Faction API: members list

1898966 [1898966]

I don't think that Stewie is saying the listing ID should be removed.

Rather, restructure the api tp include the listing IDs as arrays to make it easier to reference to.
Xiphias [187717]

If you need to have the IDs as keys for each object you could do something like this:

https://jsfiddle.net/k5f0wjdt/ - Remember to insert your API key to test.

For example:

This function will turn a 'bazaar' JSON from:

{"bazaar":{"1494547":{"cost":24500,"quantity":150},

To

{"bazaar":[{"id": 1494547, "cost": 24500, "quantity": 150}]}
tos [1976582]

I use python and order it by cost then quantity in one line.

for playerID in sorted(BazaarPrices, key=lambda x: (BazaarPrices[x]['cost'], BazaarPrices[x]['quantity'])):

My thoughts are that if it is not a major improvement no changes should be made.
Plornt [1799359]

To be honest your last point was a better reason than any given by anyone else in this thread.

That being said, please for future API endpoints use an array of objects. It doesnt matter in the grand scheme of things really it just means I dont have to go about converting from an object to an array of objects. It removes one step. It makes more sense to do it this way.