0

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

asked Jun 30, 2014 at 12:25
1
  • 1
    This question does not appear to be about parsing JSON, but about navigating nested Python dictionaries. Commented Jun 30, 2014 at 12:30

2 Answers 2

1

You have to use python syntax as follows:

>>> print response['swell']['components']['primary']['direction']
222.5
answered Jun 30, 2014 at 12:26
Sign up to request clarification or add additional context in comments.

2 Comments

Many thanks. Seems so obvious now. Appreciate the support. I hope to be able to answer some questions here i future.
@user3787389, You certainly can. I joined the site to ask a few questions and before I knew it, I was answering as well. It is actually a really good way to learn. Especially all the top quality answers from all the others on this site.
0

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

for rs in ourResult:
 print rs['components']['primary']['direction']
answered Jun 30, 2014 at 12:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.