I'm trying to access some values that are nested as an ajax response from a website.
Everything is output as one giant line that I can't manage to navigate down. However to give you an idea of what it looks like, the pprint of the dictionary is something like:
{u'd': {u'Type': None,
u'__type': u'TOPS.ajaxResponse',
u'actionOnSuccess': None,
u'data': u'{"BasicCodes":{"PRODUCTPRICES":[{"ProductId":"ProductA","CategoryId":"1","Color":"Red","Quantity":"0"},{"ProductId":"ProductA","CategoryId":"2","Color":"Blue","Quantity":"0"},{"ProductId":"ProductB","CategoryId":"1","Color":"Red","Quantity":"0"},{"ProductId":"ProductB","CategoryId":"2","Color":"Blue","Quantity":"0"}, ...and so on...
.
.
.
u'data2': None,
u'dataExtra': None,
u'errors': [],
u'general_message': None,
u'success': True}}
There are hundreds of products listed (ProductA, ProductB, etc), but all I want to do is get the number associated with "Quantity" from a specific product like ProductB, Color Blue, for example.
I load the response as a dictionary by using
json_data = urllib2.urlopen('website')
content = json_data.read()
dictionary = json.loads(content)
dictionary.keys() only outputs 'd', and dictionary.values() outputs EVERYTHING besides that, including things like u'success': True, which I would expect to be a separate key/value combo. If I try to navigate down the dictionary by using
print dictionary['d']['data']['BasicCodes']['PRODUCTPRICES'][0]['Quantity']
I get an error of TypeError: string indices must be integers.
Is the problem how I am loading the data? Or am I missing something as I navigate the keys and values?
Not sure if it is related/relevant, but I also get an error 'unicode' object has no attribute 'values' when I input
dictionary['d']['data'].values()
I'm new to Python so any help would be appreciated on this.
Because this kind of job is extremely hard in any language (not only in Python!) I created ObjectPath query language to make work with JSON documents a breeze:
Python way:
$ sudo pip install objectpath
$ python
>>> from objectpath import *
>>> with open('test1.json') as f:
... j = json.load(f)
>>> tree=Tree(j)
>>> tree.execute("$..PRODUCTPRICES[@.ProductId is "ProductA" and @.Color is "Blue"].Quantity")
[u'0']
Console way
git clone https://github.com/adriank/ObjectPath.git
cd ObjectPath/ObjectPathPy
python objectpath file.json
(or python objectpath -u URL)
>>> $..PRODUCTPRICES[@.ProductId is "ProductA" and @.Color is "Blue"].Quantity
[0]
You can do much much more with the language! It's like SQL for JSON (and other complex data structures). More at: http://adriank.github.io/ObjectPath/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With