So here is the situation: I have a json object, requested from a 3rd party source, that has a list of items. 14 of them.
I am able to display them all by count: ie. 0,1,2,3...13. This is what I used to display them.
x = my_request["jsonObject"]["items"]
for ex in range(x):
print(ex)
But each one of these items have additional attributes from within and I want to access at least one item's attribute. Let's say the one I want to access is "item_color", and according to the jSON file it is nested with other attribute.
This was my attempt:
for ex in x:
print(ex["itemInfo"]["color"])
But I get an 'int' object is not subscriptable.
Is there a way for me to display the attribute that I want to display on the console, just like how I displayed all the items by count?
Please find a screenshot of the json object I am manipulating below to get a better idea.
https://www.dropbox.com/s/apao25r9j2b22ir/Screen%20Shot%202018-07-25%20at%204.07.36%20PM.png?dl=0
Top comments (5)
Check this code:
In the screenshot you have a an array of items right ? Is something like this:
range
only accept an integer or other object that can be interpreted like one.Maybe the line when you get the items is wrong, you got the number of items not the items, how are you parsing the
JSON
from the request ?I figured out what the problem is. I had assumed that my variable x was supposed to be my object, but what it really was, was an integer.
I went back to the drawing board to locate the actual object and got it to display what I wanted.
So... problem solved!
I said that in the below comment:
Good for you @crhodes, remember always read the doc about functions that you use.
Here's a better screenshot. It should make things clear:
From line 1 to line 36, that is just one item. The a, b, c... are attribute related to the item. In fact, b was changed to itemInfo and inside itemInfo are attributes like form, shape, and color, and everything from url to payload are inside itemInfo
dropbox.com/s/w2jsmja0b83kky6/Scre...
I parse my json as a array or as a dictionary. Not really confident with my answer but I will confirm my findings once I get in front of my workload on my computer