Python JSON Parsing using json.load() and loads() (2024)

This article demonstrates how to use Python’s json.load() and json.loads() methods to read JSON data from file and String. Using the json.load() and json.loads() method, you can turn JSON encoded/formatted data into Python Types this process is known as JSON decoding. Python built-in module json provides the following two methods to decode JSON data.

Further Reading:

  • SolvePython JSON Exerciseto practice Python JSON skills

To parse JSON from URL or file, use json.load(). For parse string with JSON content, use json.loads().

Python JSON Parsing using json.load() and loads() (1)

Syntax of the json.load() and json.loads()

We can do many JSON parsing operations using the load and loads() method. First, understand it’s syntax and arguments, then we move to its usage one-by-one.

Synatx of json.load()

json.load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)Code language: Python (python)

Syntax of json.loads()

json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)Code language: Python (python)

All arguments have the same meaning in both methods.

Parameter used:

The json.load() is used to read the JSON document from file and The json.loads() is used to convert the JSON String document into the Python dictionary.

  • fp file pointer used to read a text file, binary file or a JSON file that contains a JSON document.
  • object_hook is the optional function that will be called with the result of any object literal decoded. The Python built-in json module can only handle primitives types that have a direct JSON equivalent (e.g., dictionary, lists, strings, Numbers, None, etc.). But when you want to convert JSON data into a custom Python type, we need to implement custom decoder and pass it as an object object_hook to a load() method so we can get a custom Python type in return instead of a dictionary.
  • object_pairs_hookis an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value ofobject_pairs_hookwill be used instead of the Python dictionary. This feature can also be used to implement custom decoders. Ifobject_hookis also defined, theobject_pairs_hooktakes priority.
  • parse_float is optional parameters but, if specified, will be called with the string of every JSON float and integer to be decoded. By default, this is equivalent to float(num_str).
  • parse_int if specified, it will be called with the string of every JSON int to be decoded. By default, this is equivalent to int(num_str).

We will see the use of all these parameters in detail.

json.load() to read JSON data from a file and convert it into a dictionary

Using a json.load() method, we can read JSON data from text, JSON, or binary file. The json.load() method returns data in the form of a Python dictionary. Later we use this dictionary to access and manipulate data in our application or system.

Mapping between JSON and Python entities while decoding

Please refer to the following conversion table, which is used by the json.load() and json.loads() method for the translations in decoding.

JSONPython
objectdict
arraylist
stringstr
number (int)int
number (real)float
trueTrue
falseFalse
nullNone

Now, let’s see the example. For this example, I am reading the “developer.json” file present on my hard drive. This file contains the following JSON data.

{ "name": "jane doe", "salary": 9000, "skills": [ "Raspberry pi", "Machine Learning", "Web Development" ], "email": "[email protected]", "projects": [ "Python Data Mining", "Python Data Science" ]}
Python JSON Parsing using json.load() and loads() (2)

Example

import jsonprint("Started Reading JSON file")with open("developer.json", "r") as read_file: print("Converting JSON encoded data into Python dictionary") developer = json.load(read_file) print("Decoded JSON Data From File") for key, value in developer.items(): print(key, ":", value) print("Done reading json file")Code language: Python (python)

Output:

Started Reading JSON fileConverting JSON encoded data into Python dictionaryDecoded JSON Data From Filename : jane doesalary : 9000skills : ['Raspberry pi', 'Machine Learning', 'Web Development']email : [email protected] : ['Python Data Mining', 'Python Data Science']Done reading json file

Access JSON data directly using key name

Use the following code If you want to access the JSON key directly instead of iterating the entire JSON from a file

import jsonprint("Started Reading JSON file")with open("developer.json", "r") as read_file: print("Converting JSON encoded data into Python dictionary") developer = json.load(read_file) print("Decoding JSON Data From File") print("Printing JSON values using key") print(developer["name"]) print(developer["salary"]) print(developer["skills"]) print(developer["email"]) print("Done reading json file")Code language: Python (python)

Output:

Started Reading JSON fileConverting JSON encoded data into Python dictionaryDecoding JSON Data From FilePrinting JSON values using keyjane doe9000['Raspberry pi', 'Machine Learning', 'Web Development'][email protected] reading json file

You can read the JSON data fromtext,json, ora binary file using the same way mentioned above.

json.loads() to convert JSON string to a dictionary

Sometimes we receive JSON response in string format. So to use it in our application, we need to convert JSON string into a Python dictionary. Using the json.loads() method, we can deserialize native String, byte, or bytearray instance containing a JSON document to a Python dictionary. We can refer to the conversion table mentioned at the start of an article.

import jsondeveloperJsonString = """{ "name": "jane doe", "salary": 9000, "skills": [ "Raspberry pi", "Machine Learning", "Web Development" ], "email": "[email protected]", "projects": [ "Python Data Mining", "Python Data Science" ]}"""print("Started converting JSON string document to Python dictionary")developerDict = json.loads(developerJsonString)print("Printing key and value")print(developerDict["name"])print(developerDict["salary"])print(developerDict["skills"])print(developerDict["email"])print(developerDict["projects"])print("Done converting JSON string document to a dictionary")Code language: Python (python)

Output:

Started converting JSON string document to Python dictionaryPrinting key and valuejane doe9000['Raspberry pi', 'Machine Learning', 'Web Development'][email protected]['Python Data Mining', 'Python Data Science']Done converting JSON string document to a dictionary

Parse and Retrieve nested JSON array key-values

Let’s assume that you’ve got a JSON response that looks like this:

developerInfo = """{ "id": 23, "name": "jane doe", "salary": 9000, "email": "[email protected]", "experience": {"python":5, "data Science":2}, "projectinfo": [{"id":100, "name":"Data Mining"}]}"""

For example, You want to retrieve the project name from the developer info JSON array to get to know on which project he/she is working. Let’s see now how to read nested JSON array key-values.

In this example, we are using a developer info JSON array, which has project info and experience as nested JSON data.

import jsonprint("Started reading nested JSON array")developerDict = json.loads(developerInfo)print("Project name: ", developerDict["projectinfo"][0]["name"])print("Experience: ", developerDict["experience"]["python"])print("Done reading nested JSON Array")Code language: Python (python)

Output:

Started reading nested JSON arrayProject name: Data MiningExperience: 5Done reading nested JSON Array

Load JSON into an OrderedDict

OrderedDict can be used as an input to JSON. I mean, when you dump JSON into a file or string, we can pass OrderedDict to it.
But, when we want to maintain order, we load JSON data back to an OrderedDict so we can keep the order of the keys in the file.

As we already discussed in the article, a object_pairs_hook parameter of a json.load() method is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs.

Let’ see the example now.

import jsonfrom collections import OrderedDictprint("Ordering keys")OrderedData = json.loads('{"John":1, "Emma": 2, "Ault": 3, "Brian": 4}', object_pairs_hook=OrderedDict)print("Type: ", type((OrderedData)))print(OrderedData)Code language: Python (python)

Output:

Ordering keysType: <class 'collections.OrderedDict'>OrderedDict([('John', 1), ('Emma', 2), ('Ault', 3), ('Brian', 4)])

How to use parse_float and parse_int kwarg of json.load()

As I already told parse_floatand parse_int, both are optional parameters but, if specified, will be called with the string of every JSON float and integer to be decoded. By default, this is equivalent to float(num_str) and int(num_str).

Suppose the JSON document contains many float values, and you want to round all float values to two decimal-point. In this case, we need to define a custom function that performs whatever rounding you desire. We can pass such a function to parse_float kwarg.

Also, if you wanted to perform any operation on integer values, we could write a custom function and pass it to parse_intkwarg. For example, you received leave days in the JSON document, and you want to calculate the salary to deduct.

Example

import jsondef roundFloats(salary): return round(float(salary), 2)def salartToDeduct(leaveDays): salaryPerDay = 465 return int(leaveDays) * salaryPerDayprint("Load float and int values from JSON and manipulate it")print("Started Reading JSON file")with open("developerDetails.json", "r") as read_file: developer = json.load(read_file, parse_float=roundFloats, parse_int=salartToDeduct) # after parse_float print("Salary: ", developer["salary"]) # after parse_int print("Salary to deduct: ", developer["leavedays"]) print("Done reading a JSON file")Code language: Python (python)

Output:

Load float and int values from JSON and manipulate itStarted Reading JSON fileSalary: 9250.542<class 'float'>Salary to deduct: 3Done reading a JSON file

Implement a custom JSON decoder using json.load()

The built-in json module of Python can only handle Python primitives types that have a direct JSON equivalent (e.g., dictionary, lists, strings, numbers, None, etc.).

When you execute a json.load or json.loads() method, it returns a Python dictionary. If you want toconvert JSON into a custom Python object then we can write a custom JSON decoder and pass it to the json.loads() method so we can get a custom Class object instead of a dictionary.

Let’s see how to use the JSON decoder in the load method. In this example, we will see how to use object_hook parameter of a load method.

import jsonfrom collections import namedtuplefrom json import JSONEncoderdef movieJsonDecod(movieDict): return namedtuple('X', movieDict.keys())(*movieDict.values())# class for your referenceclass Movie: def __init__(self, name, year, income): self.name = name self.year = year self.income = income# Suppose you have this json document.movieJson = """{ "name": "Interstellar", "year": 2014, "income": 7000000}"""# Parse JSON into an Movie objectmovieObj = json.loads(movieJson, object_hook=movieJsonDecod)print("After Converting JSON into Movie Object")print(movieObj.name, movieObj.year, movieObj.income)Code language: Python (python)

Output:

After Converting JSON into Movie ObjectInterstellar 2014 7000000

Also read:

  • Check if a key exists in JSON and Iterate the JSON array
  • Python Parse multiple JSON objects from file

So What Do You Think?

I want to hear from you. What do you think of this article? Or maybe I missed one of the uses of json.load() and json.loads(). Either way, let me know by leaving a comment below.

Also, try to solve the Python JSON Exercise to have a better understanding of Working with JSON Data in Python.

Python JSON Parsing using json.load() and loads() (2024)
Top Articles
"schannel: failed to receive handshake, SSL/TLS connection failed" error while using Git Clone, Pull, Push or Fetch | Bitbucket Cloud
Connect Metamask to ROP Ethereum Testnet Ropsten
Po Box 7250 Sioux Falls Sd
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Brady Hughes Justified
Occupational therapist
Jesus Calling December 1 2022
Activities and Experiments to Explore Photosynthesis in the Classroom - Project Learning Tree
Pickswise the Free Sports Handicapping Service 2023
Tribune Seymour
Jcpenney At Home Associate Kiosk
Top Hat Trailer Wiring Diagram
Goldsboro Daily News Obituaries
Craigslist Jobs Phoenix
New Mexico Craigslist Cars And Trucks - By Owner
Thotsbook Com
Sivir Urf Runes
Teenleaks Discord
Unit 33 Quiz Listening Comprehension
Inside the life of 17-year-old Charli D'Amelio, the most popular TikTok star in the world who now has her own TV show and clothing line
Lowes Undermount Kitchen Sinks
Scout Shop Massapequa
Busted Mcpherson Newspaper
Morse Road Bmv Hours
Employee Health Upmc
Www.paystubportal.com/7-11 Login
Elbert County Swap Shop
Shoe Station Store Locator
What Sells at Flea Markets: 20 Profitable Items
Unity Webgl Car Tag
Keshi with Mac Ayres and Starfall (Rescheduled from 11/1/2024) (POSTPONED) Tickets Thu, Nov 1, 2029 8:00 pm at Pechanga Arena - San Diego in San Diego, CA
Restored Republic
Club Keno Drawings
Que Si Que Si Que No Que No Lyrics
47 Orchid Varieties: Different Types of Orchids (With Pictures)
Bus Dublin : guide complet, tarifs et infos pratiques en 2024 !
Gwu Apps
Craigslist Georgia Homes For Sale By Owner
Myanswers Com Abc Resources
Pokemon Reborn Locations
Wo ein Pfand ist, ist auch Einweg
3 Zodiac Signs Whose Wishes Come True After The Pisces Moon On September 16
Mbfs Com Login
Here's Everything You Need to Know About Baby Ariel
Csgold Uva
RubberDucks Front Office
This Doctor Was Vilified After Contracting Ebola. Now He Sees History Repeating Itself With Coronavirus
Ups Customer Center Locations
Inside the Bestselling Medical Mystery 'Hidden Valley Road'
Craigslist Farm And Garden Missoula
Wayward Carbuncle Location
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 5909

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.