Python for Data Engineers

A quick look at the json module in Python

Constantin LunguUpdated 1 min read

Photo by Pankaj Patel on Unsplash

If you ever need to work with JSON files in Python, you're going to encounter the module with the same name.

It help encode to and decode from JSON. Here are the basics:

➡ json.load imports contents from a JSON file to a Python object, based on conversion rules (object -> dict, array -> list/tuple, string-> str etc)

➡ json.dump performs the opposite operation - export to a file-like object

There are also the json.loads and json.dumps functions (notice the extra s) which apply to strings instead of files.

There's also the json.tool, which can validate and pretty-format a provided json file.

You can save that prettified json into another file, as follows:

python -m json.tool ugly_format.json pretty_format.json

Input data: data.json, a users list of three objects with id, name and email: 1 John Doe johndoe@example.com, 2 Jane Smith janesmith@example.com and 3 Bob Johnson bobjohnson@example.com.

import json

# Loading JSON data

object_from_json_string = json.loads('{"a":1, "b":2, "c":[1,2,3]}')
# <class 'dict'> {'a': 1, 'b': 2, 'c': [1, 2, 3]}

object_from_file = json.load(open('data.json'))
# <class 'dict'> {'users': [{'id': 1, 'name': 'John Doe', 'email': 'johndoe@example.com'},
#                           {'id': 2, 'name': 'Jane Smith', 'email': 'janesmith@example.com'},
#                           {'id': 3, 'name': 'Bob Johnson', 'email': 'bobjohnson@example.com'}]}


# Exporting (dumping) JSON data

a_dict = {'x': 1, 'y': 2, 'z': [1,2,3] }

json_string = json.dumps(a_dict)
# <class 'str'> {"x": 1, "y": 2, "z": [1, 2, 3]}

json.dump(a_dict, open('exported_data.json', 'w'))

Output: the exported_data.json file written by json.dump contains {"x": 1, "y": 2, "z": [1, 2, 3]}.


Enjoyed this? Here are some related articles you might find useful: