Latest Code Tutorials

Python JSON parse: How To Parse JSON in Python


Python json.dump() is an inbuilt function that is used to parse JSON. The json dump() function  returns json string. We need to import the json module to work with json functions. JSON stands for ‘JavaScript Object Notation‘ is a text-based format that facilitates data interchange between diverse applications. 

For example, an application written in ASP.NET running on Windows Server can easily exchange JSON data with an application written in Python and running on Linux.  

The most common JSON entity that you will encounter is an object: a set of key-value mappings in the format shown below.

  "firstName": "Krunal",
  "lastName": "Lathiya",
  "education": "IT Engineer",
  "age": 25

Here is how you can represent an array of objects.

In this representation, each item of the array is an object. JSON a popular data format used for asynchronous browser/server communication. You can identify JSON using the following key points.

  1. The data is only a name-value pair.
  2. Data/Objects/arrays are separated by the comma.
  3. Curly braces hold an object.
  4. Square holds the array.

How to Parse JSON in Python

Python provides the json module which can be imported to any file and use to both parse JSON, as well as generate JSON from python objects and lists.

Let us see the function json.dump().

Python json dumps

Now we will learn how to convert python data to JSON data. The task is straightforward. At first import the json module. Then use the json.dump() function to decode the json data.

Let us take almost all type of data in the example and convert into JSON and print in the console.


import json

string_data = 'this is appdividend blog'
integer_data = 21
float_data = 21.19
list_data = [string_data, integer_data, float_data]
nested_list = [integer_data, float_data, list_data]
dictionary = {
    'int': integer_data,
    'str': string_data,
    'float': float_data,
    'list': list_data,
    'nested list': nested_list
print('String :', json.dumps(string_data))
print('Integer :', json.dumps(integer_data))
print('Float :', json.dumps(float_data))
print('List :', json.dumps(list_data))
print('Nested List :', json.dumps(nested_list, indent=2))

See the output below.

How To Parse JSON in Python | Python JSON Parsing Tutorial

Python JSON loads

You can easily parse JSON data to Python objects.

By using json.loads() function you can convert JSON data into Python data. Let see the following example.


import json

arrayJson = '[21, 21.19, ["this is appdividend blog", 19, 19.5]]'
objectJson = '{"a":1, "b":1.5 , "c":["this is appdividend blog", 1, 1.5]}'

list_data = json.loads(arrayJson)
dictionary_data = json.loads(objectJson)


See the below output. We get the list and dictionary data.

Python json dumps

Finally, How To Parse JSON in Python is over. Thanks for taking it.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.