Python json parse float

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

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:

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

Python JSON parsing using load and loads

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)

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)

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_hook is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the Python dictionary. This feature can also be used to implement custom decoders. If object_hook is also defined, the object_pairs_hook takes 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.

Developer JSON file

import json print("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")
Started Reading JSON file Converting JSON encoded data into Python dictionary Decoded JSON Data From File name : jane doe salary : 9000 skills : ['Raspberry pi', 'Machine Learning', 'Web Development'] email : JaneDoe@pynative.com projects : ['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 json print("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")
Started Reading JSON file Converting JSON encoded data into Python dictionary Decoding JSON Data From File Printing JSON values using key jane doe 9000 ['Raspberry pi', 'Machine Learning', 'Web Development'] JaneDoe@pynative.com Done reading json file

You can read the JSON data from text, json, or a 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 json developerJsonString = """ < "name": "jane doe", "salary": 9000, "skills": [ "Raspberry pi", "Machine Learning", "Web Development" ], "email": "JaneDoe@pynative.com", "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")
Started converting JSON string document to Python dictionary Printing key and value jane doe 9000 ['Raspberry pi', 'Machine Learning', 'Web Development'] JaneDoe@pynative.com ['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": "JaneDoe@pynative.com", "experience": , "projectinfo": [] > """

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 json print("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")
Started reading nested JSON array Project name: Data Mining Experience: 5 Done 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.

import json from collections import OrderedDict print("Ordering keys") OrderedData = json.loads('', object_pairs_hook=OrderedDict) print("Type: ", type((OrderedData))) print(OrderedData)
Ordering keys Type: 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_float and 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_int kwarg. For example, you received leave days in the JSON document, and you want to calculate the salary to deduct.

import json def roundFloats(salary): return round(float(salary), 2) def salartToDeduct(leaveDays): salaryPerDay = 465 return int(leaveDays) * salaryPerDay print("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")
Load float and int values from JSON and manipulate it Started Reading JSON file Salary: 9250.542 Salary to deduct: 3 Done 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 to convert 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 json from collections import namedtuple from json import JSONEncoder def movieJsonDecod(movieDict): return namedtuple('X', movieDict.keys())(*movieDict.values()) # class for your reference class 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 object movieObj = json.loads(movieJson, object_hook=movieJsonDecod) print("After Converting JSON into Movie Object") print(movieObj.name, movieObj.year, movieObj.income)
After Converting JSON into Movie Object Interstellar 2014 7000000

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.

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

About Vishal

I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ

Источник

Читайте также:  Определить директорию файла python
Оцените статью