-2

I have JSON

[{"id": 1, "title": "ble", "description": "ble", "done": true}, {"id":2, "title": "2", "description": "2", "done": true}]

and I want to load it into a Python 3 dictionary, so then I can reference it via this id from JSON like print(json[2]) would print it one item.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Andrew
  • 1,037
  • 9
  • 17

1 Answers1

0

Use the built-in json package: https://docs.python.org/3/library/json.html

import json

# some JSON:
stringifiedJSON = '[{"id": 1, "title": "ble", "description": "ble", "done": true}, {"id":2, "title": "2", "description": "2", "done": true}]'

# parse x:
parsedJSON = json.loads(stringifiedJSON) # a dict representing the JSON

# access specific field
print(parsedJson[1])

Output: >>> { "id":2, "title":"2", "description":"2", "done":true }

Note that it would be impossible to access the key 2 in the JSON string sample you provided, since it is an array with only 2 elements (indices are zero-based).

zr0gravity7
  • 2,917
  • 1
  • 12
  • 33