JSON elements in Python
All JSON elements have a Python equivalent as shown below:
This makes manipulation of JSON data in Python simple and allows us to access elements and convert them to Python code easily.
Converting to Python
To do anything useful with JSON files in Python, you first must import the json library. To do this, add “import json” to the beginning of your Python file:
To open your JSON file in read mode, use the following format:
Once your file is loaded you can access elements and values by typing your variable name and the “address” of the element like below:
The console will then return the name of the second element in
“fruit.json”. For larger JSON files, which take on a tree structure, you
can access each child of a parent element by using this format:
- For name/value pairs, to get the value, you use [“name"].
- For list values, it is like array indexing [0], [1], [2] etc.