What is JSON?
Python – Read, Write, and Parse JSON
Because JSON is language independent, Python objects can also be converted to JSON. Likewise, if Python receives data in JSON format, it can seamlessly convert it into Python objects. JSON is lightweight. You need only import JSON in Python to make use of Python’s reading JSON, writing JSON, and parsing JSON functionality. JSON is a built-in module in Python and you need only type import json at the top of your Python with JSON code to get started.
Why would one use JSON with Python? Well, if you are working on a web-based project which has to do with reading data, chances are you will have to stumble upon a Python-way out of the situation. Python is a language which can be used for so many different purposes and its growing functionality means that it is inevitable that you will encounter it in your programming journey. The support that Python gives to different dynamics of programming means that is a piece of cake to work with JSON in this language.
Basic JSON Module Functions in Python
If you have used Python in a considerable amount in the past, then you must be familiar with a data structure known as the dictionary. In fact, it is quite probable that dictionaries have been your best friends when working in Python. Their easy-to-interpret format makes them the most sensible data structure to use when working with custom objects which require attributes of different types. And believe us when we say this that dictionaries are used insanely frequently in Python!
For example, if you want to use an Item object, you may create a dictionary like:
Now that you have seen what a dictionary looks like in Python code and when it is printed out, I’m sure its format makes a lot more sense now than when you’d first stumble upon that information enclosed in curly brackets.
Here’s where things start to get a little more relevant. Buckle up! Because we’re going to reveal the greatest intersection between Python and JSON. Remember the format of a dictionary in Python? That little code snippet with the braces shown above? Now observe the picture below:
Notice something? How can you not? It’s exactly the same! Python dictionaries and JSON text are in essentially the same format. You can probably deduce from just this what we are going to establish as the article progresses – that it is epically simple to “dump” Python dictionaries into JSON files. Dictionaries aren’t the only Python objects which can be converted into JSON text. Lists, tuples, booleans, strings, ints, and the list goes on are all interchangeable with JSON. One thing however which you might have failed to notice in the two pictures above is the quotation marks. JSON uses double quotation marks only, unlike Python. You might want to remember this detail to save yourself from an episode of “Where’s the damn bug this time??!”.
The first thing you have to do is import Python’s built-in module for JSON.
Lets us first start by creating JSON from a Python dictionary. Writing JSON uses the method json.dumps(). This method returns a JSON string. The code screenshot below shows a dictionary converted to JSON and its output shown in Jupyter Notebook.
Now, let’s write this Python dictionary converted to JSON into a JSON file. The code snippet below shows this being done by using json.dump(). This will create a text file (txt extension file) with the JSON string written to it.
Here’s what the resulting file looks like:
Now that you are familiar with the basic functionality of the dumps method, you can safely read up on how to better use its parameters. It has a parameter for data ordering called sort_keys. You can set the delimiter instead of using the default with the separators parameter. Indent can also be specified.
So here you have it! It is this easy to work with JSON files in Python! Use this guide and Python’s documentation to help you get started.