Python JSON

Python JSON

JavaScript Object Notation or JSON is one of the most popular formats to store structured data used by many programming languages.

It was originally developed for JavaScript but it became quite popular, and now other programming languages including Python also support it.

Python has a built-in module named json to deal with JSON file format.

So, what is the big deal with JSON? Well,

  • For other programming languages and Python, parsing in browsers of JSON objects is pretty fast which is essential when you receive and transmit a lot of data between a client and a web server.
  • Also, when a user closes a program, you always want to store the information that the user entered, and JSON makes it easier because it is universal and uses dictionaries and lists to store information that you were going to use anyways.

JSON objects are much like Python dictionaries, which means that they all have some keys and those keys have values and this makes the object easier to work with.

Before we go and start working on JSON, please note that it is quite common to save JSON objects as a file.

The json Module

We have to use Python’s json module to work with JSON objects or JSON strings, so let’s import the module to work with JSON.

Example

Python Json-The json Module

Now, let us create some real JSON objects!

Python dump and load for JSON

Saving a object in JSON is called dumping, and there is a function in the json module named dump() to do it. Now, if you want to read the saved file, you will have to use the load() function in the Python json module.

Python json.dumps()

json.dumps() function in Python is used to convert a Python object to a json string. Let us use this data and convert it into a JSON object.

Example

Python Json-Python json dumps

Python json.load()

JSON files can be read using the json.load() method in Python. Let us take an example of a file named “person.json” inside your local machine that contains the following data:

Data:

person = {“Name” : “Bill”, “fav_colors”: [“blue”, “red”, “black”, “white”]}

Save this in the same directory with the file type extension of json type.

Let’s open the data we created using the json.load() method above.

Example

Python Json-Python json load

To open a json file, you will have to create one too, in Python it is done by the json.dump() method.

Python json.dump()

The json.dump() function has two arguments, first one is the filename, and the second one that specifies the file object that it will use to store the information.

Let’s create a json object and save or dump it.

Example

Python Json - Python json dump

How json dump code work?

  1. First, we define the name of our file using the filename variable.

  2. Using the built-in open function, we open our file, the open function takes two parameters, filename and the mode we want to select to open our file. In this case, we choose the write (‘w’) mode which tells Python that we want to write the file in the memory.

  3. Now, we use json.dump() method to create and save our JSON file if the file does not exist in the memory.

  4. Next, we simply load our file using the load() method.

JSON Pretty Print

If you open your file in the text editor it will be printed in a single line, even though we used line spacing and indentation in our code, so how to prevent it? The answer is to use the indentย parameter in the json.dump() method.

The indent parameter’s value can be set equal to double quotes (“”). This simple double quote makes the every key-value pair in the JSON file to appear in a new line and you can also set the value of the indent to an integer to specify the indent size.

Example

Python JSON - JSON Pretty Print

As you can see the JSON file’s key-value pairs are now in new lines. Pretty Print is easy to implement using the JSON Python module.

We converted the person dictionary into a string using the single quotation marks to make the loading process work out without the use of the file handling tool for Python.

Python JSON with Non-standard Keys

You cannot create a JSON object with the keys of non-standard types like Lists, tuples, etc. The Python compiler will show an error if you try to do it.

Example

Python Json non standard keys

Compiling the code given an error that says [“Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”] is not a string or one of the common types which is indeed true for it.

However, by setting the value of the parameter skipkeys = True you can avoid this error.

Example

Python Json non standard keys

The file got created seamlessly this time.

The functions json.dump() and json.dumps contribute a lot in JSON formatting in Python with lots of formatting parameters.

More Parameters:

  • sort_keys: If set equal to True, this will set the keys in the ascending order. The default value is set equal to False

  • separator: This parameter can take two values. Both of the values are enclosed in brackets and both are separated by a comma.

First value specifies the symbol that will separate keys and values in the JSON file, and the second specifies the symbol to separate a value from its key.

JSON Save User Information

Let us say you have an application, and you want to save the application user’s data, so that the next time they visit, you already have the data saved.

This way you will not have to ask for the data from the user again. You can do as below:

Python Json Save User Information

We ask for user’s information using the input() function and then save it for the upcoming visits on the application. So easy, right?

Python JSON Parsing

JSON objects and files can be converted to other storage types. There are many other ways data can be received from a web browser or a web client, for example, XML, CSV, etc.

Let us how you can convert JSON into these data storage types:

JSON to XML

XML stands for extensible markup language and it is designed for data storage and similar to JSON, it is also a case sensitive language. XML is quite similar to HTML and it makes you able to define markup elements.

The file that stores XML data format is of type .xml. There are many common things between JSON and XML, for example, both formats are very popular and can be used by many programming languages.

However, there is one big difference, JSON can be parsed using functions in different programming languages, but XML is only parsed using an XML parsing function.

This is how you can convert JSON file format to XML using Python. For it, you will need the ElementTree module from the xml.etree and of course the Python JSON module.

First, we will create data for our JSON file and save it.

Example

Python Json - Json to XML

To convert the data into XML, you should know a few things about XML and our code:

  • XML data files have exactly one root element.
  • root is the name of the variable in our code where that one root element is saved.
  • There are subelements of the root element and they are formatted in a format called XML tree.
  • XML does not support integer elements, so we would have to convert all of our integer elements into string.

Now, let’s proceed.

Let’s read our data now, assuming that your file is saved in the same directory.

Example

Python Json - Json to XML

Let’s understand the code above:

  1. We open our saved file in the read mode and assign all of the data of the JSON object to the pizza variable.
  2. Now, we create the root element for our dataset, which we name as pizza because the data set contains information about a pizza.
  3. xm.SubElement() method is used to assign sub elements to the parent element, we create a sub element named Type and assign the value of the JSON object “Type” to it. Here, “text” keyword is used to convert the XML data to string.
  4. Our “Price” category object in the JSON file has multiple categories, so we will have to create new sub elements for it. We use a for loop to iterate through the “Price” object, and using it create sub elements and assign appropriate values.
  5. Now we create a tree for the XML data using the ElementTree() method.
  6. Finally, we create and save the file as “pizza.xml”

This is how we can convert JSON to XML. Now let’s understand how to convert JSON to CSV file.

JSON to CSV

CSV or Comma Separated Values is a format to store data in the form of a table. Each value is separated by a comma and it is a fairly simple type of data storage format.

There are headers as the name of the categories of the data, and those headers may contain one or more than one homogenous set of values (it is recommended and conventional, not necessary).

For this task, let us use an appropriate dataset.

Example

Python Json - Json to Csv-creating data

After creating the data, now, let’s save it!

Example

Python Json - Json to Csv-saving the data

You will need to import the in-built CSV module along with the json module to convert JSON to CSV in Python.

Example

Python Json - Json to Csv -conversion

How the code is working:

  1. We give our CSV data a title “data_title” and assign it to the main object of our JSON file.
  2. A new csv file is created in the write mode to add JSON values to it.
  3. We instantize a writer object using the writer class in the csv module. This will be used later to write into our csv file.
  4. A count variable to keep track of headers.
  5. If the value of count is equal to zero, then we set the value of header to the key values, the key() function is used to access keys.

    Next, we use the writer object to insert row values in the header.

    The counter is increased after we have assigned all the header values.

  6. After creating all the headers, we use our writer function to assign the row values.
    As we know that a JSON file is similar to a dictionary, therefore, we can use the values() method to access values of the JSON file.

You saw the implementation of JSON to CSV conversion.

JSON to Dictionary

The JSON objects are already of type dictionary. Therefore, you do not have to convert a JSON file to a dictionary in Python. You just have to load the data and assign the variable. Let’s see how.

Example

Python Json - Json to dictionary

As you can see the type() function returned the type to be a dictionary!

Tutorials for all brains!