2

I'd like to parse

{"ticker":{"high":31.9099,"low":22.5,"vol":108468,"buy":29.61,"sell":30,"last":29.61}}

and end up with:

last = 29.61

but I don't know where to start parsing python :(

6
  • 10
    I really don't understand why these question keep getting asked... typing "parse json python" into google leads straight into the Python json package. From there it should be straightforward. Commented Jun 9, 2011 at 1:21
  • 2
    @I82Much, Embrace the non-googlers Commented Jun 9, 2011 at 1:34
  • @Mike Pennington: Sure, there's plenty of room for non-googlers. They can use Bing instead. Or they can just fire up their Python library manual contents page and type Ctrl-Fjson Commented Jun 9, 2011 at 2:01
  • 1
    @Mike thanks for the link to the discussion. I understand being welcoming etc., but I don't think people's first thought when they hit any snag should be to post a new StackOverflow question. Instead people should invest at least a modicum of effort into finding the answer on their own. That's my opinion at all. Commented Jun 9, 2011 at 15:03
  • 2
    I was less informed back in 2011 - I understand the utility of these types of questions now. Commented Jan 18, 2013 at 1:31

2 Answers 2

13
>>> text = '''{"ticker":{"high":31.9099,"low":22.5,"vol":108468,"buy":29.61,"sell":30,"last":29.61}}'''
>>> json.loads(text)
{u'ticker': {u'sell': 30, u'buy': 29.609999999999999, u'last': 29.609999999999999, u'vol': 108468, u'high': 31.9099, u'low': 22.5}}
>>> json.loads(text)[u'ticker'][u'last']
29.609999999999999

Or use simplejson with older versions of Python.

Sign up to request clarification or add additional context in comments.

2 Comments

Why is it u'ticker' instead of ticker?
@KshitizSharma: Because 'ticker' is bytes, not text.
2

I'm not sure, but I thought I should post this here in case someone else finds it useful. There is a nice post here on Parsing JSON in Python - it is small be shows how you could use it in different scenarios.

Good luck!

Comments

Your Answer

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.