Skip to main content

Command Palette

Search for a command to run...

A quick look at the json module in Python

Updated
1 min read
A quick look at the json module in Python

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

Found it useful? Subscribe to my Analytics newsletter at notjustsql.com.


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

More from this blog

D

Datawise: A blog on SQL, BigQuery, Analytics, Python Tips and Tricks

204 posts

Data Engineer with a passion for transforming complex data landscapes into insightful stories. Here on my blog, I share insights, challenges, and the ever-evolving dance of technology and business.