Loading...

How to Parse JSON with Python

Avatar
Chris Prosser

Are you looking for ways to parse JSON data using Python? Then you are on the right page as the article below shows you how to do that and even more JSON handling techniques in Python.

If you are a web developer using Python for backend development, you’ll have heard of the JSON file or data format. This is a human-readable and writable format that machines can also easily read and write. It is a subset or an extension of the Javascript library. However, due to its usage and versatility, it is currently a language-independent text format as all programming languages provide support for it.

The major use case for this text format is for transferring data from client to server and server to client. It is also used for transferring data between two programming languages without the need to use any language-specific text/data format and can be used as a standalone data format or for storing configuration details. As a Python developer, knowing how to handle and parse JSON with Python will help greatly since you will handle web request data in JSON and also send requests in JSON in the case of APIs. In this article, I will show you how to parse JSON with Python.

JSON STRING FORMAT

The closest text format in Python to JSON is the Python dictionary. It can be used to hold the same data types JSON holds which include strings, lists, null, numbers (int, float, and decimal) objects, and boolean values. However, it is still not JSON. JSON in Python is presented as a dictionary surrounded by (‘) as in the case of a string. Below is an example of what JSON looks like in Python programming.

user = {"full name": "Cambell Johnson", "phone": 8103568353, "address": "34 Yanga Boulevard, NY", "age": 35, "profession": "coder"}

print(type(user))

The above will print which suggests the type is a dictionary. You can access all of the keys, and values, or even get a specific value such as the phone number using standard Python dic methods. However, this can only work in Python. If you need to transfer this data to the front end which another programming language will be used to access it, you can’t send it like this as Javascript, Java, or any other language that is used in the front end (web and mobile app) don’t understand this.  You will need to convert it to JSON.

Likewise, Javascript has its dictionary-like data type known as an object. This also isn’t supported by Python. However, both languages read and write JSON. For Python, all you need to do is to wrap a dictionary in (‘dictionary’) and you have converted it into JSON provided it does not contain any unsupported data type.

user = '{"full name": "Cambell Johnson", "phone": 8103568353, "address": "34 Yanga Boulevard, NY", "age": 35, "profession": "coder"}'

print(type(user))

When you run the code, instead of the response, you will get the . So, yes, JSON will appear as a string. And when you want to send a dictionary from your Python script to the front end, you will also have to convert it to a string-like format.

Parsing JSON in Python

From the above format, you can see that JSON appears as a string in Python. For you to parse it, you will need to convert it into the dictionary and you need the JSON module for that. This Python library is available in the Python standard library and as such, you do not need to install it before using it. All you have to do is to import it. Below is a code showing how to import and use JSON text.

import json

user = '{"full name": "Cambell Johnson", "phone": 8103568353, "address": "34 Yanga Boulevard, NY", "age": 35, "profession": "coder"}'

user_dict = json.loads(user)

print(type(user_dict))

As you can see above, I imported the JSON library using the “import json” command and then went ahead to use the loads method to convert the JSON to dictionary. That’s the keywords here (JSON to dictionary data type). When you convert JSON, what you do is convert it to dictionary data type which is the format you understand.

It is important you know that when you also send JSON (dictionary content to JSON) to the frontend, Javascript also needs to convert it to an object using json.parse. When it needs to send you data from the front end, it uses another method json.stringify to convert objects to JSON.

Now how do you parse out data after converting JSON to dictionary? You access the data points as an access data point in a dictionary. For example, let's say you want to access the age of the user. You access it using the key(age) as done below.

print(user_dict["age"])

The above will print out 35 on the console. Now, let's parse out all of the values.

print(list(user_dict.values()))

As you can see, I use the user_dict.values() to parse and retrieve all of the values (minus the keys). However, this is not a regular Python list, and as such, no support for index. I wrapped that method in the list() function to convert it to a real Python list. You can also parse only the keys and return using the below method.

print(list(user_dict.keys()))

How to Edit JSON with Python

Why you can parse JSON and get the data in it, but you can’t edit the original JSON. However, you can edit the data you parse and convert it back into JSON. This is how you can edit JSON and send it back to a user. Using the same data we worked with earlier. Let's say we want to edit the age and change it to 40, we can do that by modifying the dictionary and then converting it to JSON using the json.dumps method. Below is how to do that.

import json

user = '{"full name": "Cambell Johnson", "phone": 8103568353, "address": "34 Yanga Boulevard, NY", "age": 35, "profession": "coder"}'

user_dict = json.loads(user)

user_dict["age"] = 40

user = json.dumps(user_dict)

print(user)

If you run the above, you will see the age has changed from 35 to 40. You will also notice you can no longer access the values. To find out why, just run the print(type(user)) code and you will see that it will display . This means it is now in JSON, ready to be sent back to the front end.

How to Parse JSON File in Python

Most times when we talk of JSON, our mind goes to the data format we used in sending data back and from — from and from a server. It is the most popular for this kind of task. However, that is not its only task. Some use JSON as a data format. There are some web frameworks including ExpressJS store their configuration data in a JSON format in a file with the .json extension. If you have to deal with data in the .json file, all you have to do is load the file and read its content then parse it as done above. Let's say how to do that in practice below.

First, this is the content of the .json file I will use.

{

    "name": "John",

    "age": 25,

    "city": "Sampleville",

    "married": false,

    "hobbies": ["reading", "traveling", "programming"],

    "address": {

      "street": "123 Main Street",

      "city": "Sample City",

      "postal_code": "12345"

    }

  }

Now take a look at how I read the file and parse it.

with open("json_example.json", "r") as f:

  # Read the file contents

  file_data = f.read()

json_data = json.loads(file_data)

print(json_data["name"])

As you can see above, I read the file content using the standard method of reading a file and stored the content which is in string form to the file_data variable. I then use the json.loads method to convert the JSON to a dictionary and then print the name on the console.

Common Errors Experienced While Parsing JSON with Python

From the above, there is no error experienced. This is because the data used above are test data either generated by me or downloaded from sample data sites. In a production environment where you interact with real data, you will experience some errors while parsing JSON. Below are some of the errors

  • JSONDecodeError

When you see this error, it is because your JSON data is invalid. There are many reasons why Python will consider your code invalid and those, can’t decode it thereby throwing this error. The reason can range from serious issues to small issues such as a missing or extra comma. Make sure the JSON you want to parse follows the syntax of JSON else, this error will be thrown.

  • TypeError

Let's say you parsed out a supposed number from JSON and try to add it or carry out a mathematical operator with it, however, even though it appears as a number, it is a string, you will get the TypeError. So, the TypeError occurs when there is a mismatch between the expected data type and the actual one. You will need to carry out some type of conversion to solve this problem.

  • KeyError

If you try to parse data using a key that does not exist, you will get this error. This is common in situations where you have a list of objects in the JSON and most of them contain a particular key. However, as you parse and get to an object that does not contain the key, this error will be thrown. If you don’t want this error thrown, instead of using the object[“key”] approach, you should use the object.get(“key”, None).

  • AttributeError

The AttributeError is thrown when you try to access data or parse data from JSON without converting it to a dictionary using the json.loads method. This error is thrown because JSON is treated as a string before conversion. So if you try to access data from it, you will get this error, suggesting the string does not contain such an attribute.

  • NameError

Did you forget to import the JSON library? You will experience the NameError. Ordinarily, if you were using an IDE, this should be caught without even running the code. But should it fly under the radar and not be caught, you will experience this error. All you have to do is import JSON and you are good to go.

Conclusion

Python is good at handling different file formats including text formats like JSON. You can use it to parse JSON which is incredibly useful for accessing API data as well as handling user input data sent from the front end. In this article, I have been able to discuss and show you practically how to parse JSON with Python. On your own part, you need to be sure the JSON you want to parse is correct as not all exceptions regarding JSON can be parsed gracefully.

FAQs

If you have JSON and you need to save it in a text field (not JSON field), you shouldn’t save it as it is as you will face some challenges when it is time to parse the JSON after retrieving from the database. The best thing to do is to stringify the JSON using the json.dumps method in Python. With this, all you have to do is to use the json.loads method to get it back into JSON without facing some of the challenges you will face if you were to save without stringifying the JSON.

The most popular data format used by API is JSON. If the data returned by an API is in JSON format, you have little to worry about. All you have to do is use the json.loads method to convert the string to a dictionary. After the conversion, you can now access the key or value as you would access them in a dictionary as that is what it has been converted into.

Depending on the data a JSON file holds, it can be really complex. Regardless of how simple and complex a JSON file is, you can still read and parse it in Python using the JSON module. All you have to do is to read the JSON file and feed its content to the json.loads method for parsing. If there is no error in the JSON, you can then access it as a dictionary or list, depending on how the JSON content has been structured.

Top

Top