user3787389
user3787389

Reputation: 43

Parse JSON in python to a dictionary

A bit lost after much research. My code below parses the JSON to a dictionary I have thought using json load

response = json.load(MSW) # -->Will take a JSON String & Turn it into a python dict

Using the iteration below I return a series like this which is fine

{u'swell': {u'components': {u'primary': {u'direction': 222.5}}}}
{u'swell': {u'components': {u'primary': {u'direction': 221.94}}}}

ourResult = response
for rs in ourResult:
    print rs

But how oh how do I access the 222.5 value. The above appears to just be one long string eg response[1] and not a dictionary structure at all.

In short all I need is the numerical value (which I assume is a part of that sting) so I can test conditions in the rest of my code. Is is a dictionary? With thanks as new and lost

Upvotes: 0

Views: 588

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1121894

Just access the nested dictionaries, unwrapping each layer with an additional key:

for rs in ourResult:
    print rs['components']['primary']['direction']

Upvotes: 0

sshashank124
sshashank124

Reputation: 32189

You have to use python syntax as follows:

>>> print response['swell']['components']['primary']['direction']
222.5

Upvotes: 1

Related Questions