Convert a JSON String to a Dictionary in Python

In this Python tutorial, we will learn how to convert a JSON string to a dictionary.

Table Of Contents

What is JSON?

JSON stands for Javascript object notation. In Python, we can create JSON strings within quotes (”’ …. ”’).

Convert a JSON file to a dictionary using json.load()

In this case, we are converting json file to a dictionary using the load() method. It takes a JSON file from an external source and loads it into a dictionary.

Syntax:

Advertisements
with open('file_name.json', 'r') as data:
    result = json.load(data)

Here, file_name is the name of the json file, and data act as a pointer that refers to the json. We will load each value present in the JSON file and finally, we will store it into a result variable.

Example:

Here, we will consider a json string in a file named – JSON_DATA that has the json string:

{
  "tutorial_id": 1,
  "tutorial_name": "Python",
  "tutorial_price": 340,
  "tutorial_duration": "2 weeks"
}

Now we will convert this into a dictionary and return the type using the type() method.

import json

# Load the JSON_DATA.json
with open('JSON_DATA.json', 'r') as data:
    result = json.load(data)

# Get the json data
print(result)

# Get the type of the result
print(type(result))

Output:

{'tutorial_id': 1,
 'tutorial_name': 'Python',
 'tutorial_price': 340,
 'tutorial_duration': '2 weeks'}

<class 'dict'>

The contents from JSON file got loaded into a dictionary. It is a key-value pair data structure.

Convert a Nested JSON file to a dictionary using json.load()

Now we will convert a Nested JSON string to a dictionary using json.load() method. Consider the nested json file JSON_DATA_NESTED.json with following content,

{
  "tutorial_id": [1,2,3,4,5],
  "tutorial_name": ["C","Python","CPP","Java","Other"],
  "tutorial_price": [120,340,500,125,340],
  "tutorial_duration": ["2 weeks","3 weeks","4 weeks","3 weeks","2 weeks"]
}

Example:

Convert the above json file into a dictionary.

import json

# Load the JSON_DATA_NESTED.json
with open('JSON_DATA_NESTED.json', 'r') as data:
    result = json.load(data)

# Get the json data
print(result)

# Get the type of the result
print(type(result))

Output:

{'tutorial_id': [1, 2, 3, 4, 5], 
 'tutorial_name': ['C', 'Python', 'CPP', 'Java', 'Other'],
 'tutorial_price': [120, 340, 500, 125, 340], 
 'tutorial_duration': ['2 weeks', '3 weeks', '4 weeks', '3 weeks', '2 weeks']}

<class 'dict'>

You can see that the type ‘dict’ is returned, and each key has 5 values associated with it through a list.

Convert a JSON String to a dictionary using json.loads()

In this case, we are converting json string to a dictionary using the loads() method. It takes a JSON string and loads it into a dictionary.

Syntax:

dictObj = json.loads(data)

Here, the argument data is the json string and it returns a dictionary object.

Example:

Here, we will consider a json string and convert it into a dictionary.

import json

# Consider the json string
data ='''
{
  "tutorial_id": 1,
  "tutorial_name": "Python",
  "tutorial_price": 340,
  "tutorial_duration": "2 weeks"
}
'''

result = json.loads(data)

# Get the json data
print(result)

# Get the type of the result
print(type(result))

Output:

{'tutorial_id': 1,
 'tutorial_name': 'Python', 
 'tutorial_price': 340, 
 'tutorial_duration': '2 weeks'}

<class 'dict'>

It returned a string loaded with the json string.

Convert a Nested JSON String to a dictionary using json.loads()

Now we will convert a Nested JSON string to a dictionary using json.loads() method.

Example:

import json

# Consider the json string
data ='''
{
  "tutorial_id": [1,2,3,4,5],
  "tutorial_name": ["C","Python","CPP","Java","Other"],
  "tutorial_price": [120,340,500,125,340],
  "tutorial_duration": ["2 weeks","3 weeks","4 weeks","3 weeks","2 weeks"]
}
'''

result = json.loads(data)

# Get the json data
print(result)

# Get the type of the result
print(type(result))

Output:

{'tutorial_id': [1, 2, 3, 4, 5], 
 'tutorial_name': ['C', 'Python', 'CPP', 'Java', 'Other'], 
 'tutorial_price': [120, 340, 500, 125, 340], 
 'tutorial_duration': ['2 weeks', '3 weeks', '4 weeks', '3 weeks', '2 weeks']}

<class 'dict'>

It converted the nested json string to a dictionary object. Where each key has a list of values associated with it.

Summary

In this article, we saw two ways of converting a JSON file/string to a dictionary. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top