External Exam Download Resources Web Applications Games Recycle Bin

JavaScript Object Notation (JSON)

JSON stands for JavaScript Object Notation.

JSON is unlike XML because its values can be list data types (plus its easier to read and write).

The following windows contain a JSON file (food.json) and a Python script that can parse this food.json file.

Ensure both files are saved in the same folder location:

food_json.json

{
  "catering": {
    "year8": {
      "sausages": "heaps",
      "bread_loaf": 13,
      "sides": ["salad","sauce"]
    },
    "year9": null,
    "year10": {
      "cookies": 170
    }
  }
}

food.py

import json
jsonfile = open('food.json')
data = json.load(jsonfile)

print(data["catering"]["year10"]) #{'cookies': 170}
print(data["catering"]["year8"]["sausages"]) #heaps
print(data["catering"]["year8"]["sides"][1]) #sauce

for key in data["catering"]:
    print(key) #year8 year9 year10