JSON elements in Python

All JSON elements have a Python equivalent as shown below:

Python
JSON
dict
Object
list
Array
tuple
Array
str
String
int
Number
float
Number
True
true
False
false
None
null

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:

'import json' Statement

To open your JSON file in read mode, use the following format:

Opening a JSON file

Once your file is loaded you can access elements and values by typing your variable name and the “address” of the element like below:

Printing an Element



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:

file_name[parent][child][grand_child][and_so_on]

  • For name/value pairs, to get the value, you use [“name"].
  • For list values, it is like array indexing [0], [1], [2] etc.