Latest Code Tutorials

How To Parse JSON in Python | Python JSON Parsing Tutorial


In this tutorial, we will see How To Parse JSON in Python. JSON stands for ‘JavaScript Object Notation‘ is a text-based format which 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/Object/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 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 | Python JSON Parsing Tutorial is over. Thanks for taking.


Leave A Reply

Your email address will not be published.

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