Working with JSON data in Python

In this blog, we will discuss how to parse, read and write JSON in python with the help of examples. Also, how to convert a python Dictionary to JSON. JavaScript Object Notation is often used when data is sent from a server to a web application.

Parse JSON in Python

1.  JSON to Python Dictionary

json module in python used to parse JSON objects and JSON files into a python   dictionary.  Json.loads() method converts JSON objects into Python dictionaries.

import json

employee = '{"name": "Robert", "address": {"country": "USA", "city": "New York"}, "languages": ["English", "Italian", "Korean"]}'
employee_dict = json.loads(employee)

print("Employee Dictionary::  ",employee_dict)
print("Type:: ",type(employee_dict))
print("\nAddress after parsing JSON::  ",employee_dict['address'])
print("\nLanguages after parsing JSON::  ",employee_dict['languages'])
Output:
 Employee Dictionary::   {'name': 'Robert', 'address': {'country': 'USA', 'city': 'New York'}, 'languages': ['English', 'Italian', 'Korean']}
Type::  <class 'dict'>

Address after parsing JSON::   {'country': 'USA', 'city': 'New York'}

Languages after parsing JSON::   ['English', 'Italian', 'Korean']

2.  Parse Using JSON file

json.load() method is used to parse JSON files. Suppose, you have error_config.json which contains a sample JSON object. open() method is used to read JSON files.

error_config.json
{
  "invalid_email": {
    "parameter": "email",
    "code": "invalid",
    "message": "The email address you entered is incorrect."
  }
}
Code
import json

general_error_config_json = open('path_to_file/error_config.json')
data = json.load(general_error_config_json)
print(data)
Output
{'invalid_email': {'parameter': 'email', 'code': 'invalid', 'message': 'The email address you entered is incorrect.'}}

Python Dictionary to JSON

1. Convert dict to JSON

json.dumps() function converts a Python dictionary into a json string.

import json

employee_dict = {'name': 'Robert', 
'address': {'country': 'USA', 'city': 'New York'}, 
'languages': ['English', 'Italian', 'Korean']}
employee_json = json.dumps(employee_dict)
print(employee_json)
Output
{"name": "Robert", "address": {"country": "USA", "city": "New York"}, "languages": ["English", "Italian", "Korean"]}

2. Writing JSON to a file

json.dump() function in the json module writes the python dictionary to a file in the form of JSON without needing to convert it into an actual JSON object. json.dump() method takes 2 parameters. dictionary is a python dictionary which you want to convert into a JSON object. file pointer is used to write JSON formatted data into a file.

import json

dictionary ={
  "name": "Robert", 
  "address": {
    "country": 
    "USA", 
    "city": "New York"
    },
   "languages": ["English", "Italian", "Korean"]
  }

with open("sample.json", "w") as outputfile:
    json.dump(dictionary, outputfile)
sample.json
{"name": "Robert", "address": {"country": "USA", "city": "New York"}, "languages": ["English", "Italian", "Korean"]}



Shraddha Falane

Shraddha Falane