1

If I have JSON data stored in a string called 'data' (e.g. the example below) how do I access specific information (such as messages->unread or pokes->most_recent)?

{
  "messages": {
    "unread": 0,
    "most_recent": 1300047276
  },
  "pokes": {
    "unread": 0,
    "most_recent": 0
  },
  "shares": {
    "unread": 0,
    "most_recent": 0
  },
  "friend_requests": [],
  "group_invites": [],
  "event_invites": []
}

I'd like something like data['messages']['unread'] to work - but of course it won't when my data is stored as a string!

8128
  • 969
  • 1
  • 8
  • 27

2 Answers2

5

JSON parser is bundled with Python since 2.6: json module. To unserialize a string, use json.loads, e.g.

import json
data = json.loads(...)

You can also load directly from a file-like object with json.load.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
0

You run the string through a JSON parser to turn it into a suitable data structure for whatever language you are using (sets, arrays, strings, etc in Python) . There are a number listed near the bottom of http://json.org/ for a variety of languages.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335