Parsing JSON in python

by Alex
Parsing JSON in python

The json module from the standard Python programming language library is a fairly efficient way to interact with the JavaScript Object Notation format. The functions of this module allow you to easily encode and decode information by working with different JSON objects. This greatly improves the usability of creating web applications in Python.

What is JSON?

JSON (JavaScript Object Notation) is an extremely common data format that is represented in text form. As you can guess from the name, JSON is originally based on the popular JavaScript programming language, but despite that it works great on different platforms because of its versatility and simplicity. The format gives the programmer the ability to serialize information structures for subsequent transfer between applications. An example of this could well be the exchange of textual information about customers in an online store between a browser and a server. Information in JSON format can be represented in two forms:

  • A sequence of pairs with keys and corresponding values;
  • Just an ordered set of values.

Generally, any high-level programming language supports these data structures. Values that are passed to JSON can be objects, strings, numbers, one-dimensional arrays, and literals (true, false, null). Python supports working with JSON format, thanks to the json module and methods for encoding and decoding data. This makes it easy to receive and send information in a readable form.

Saving data in JSON

To write information in JSON format using the Python language tools, you should first of all enable the json module, using the import json command at the beginning of your program’s code file. The dumps method is responsible for automatically packaging the data in JSON, taking a variable which contains all the necessary information. The following example shows the encoding of a dictionary called dictData. It contains some information about the user of the Internet portal, such as identification code, login, password, full name, phone number, e-mail address and activity data. These values are represented as normal strings as well as integers and True/False boolean literals.

import json

dictData = { “ID” : 210450,
“login” : “admin”,
“name” : “John Smith”,
“password” : “root”,
“phone” : 5550505,
“email” “[email protected]”,
“online” : True }
jsonData = json.dumps(dictData)
print(jsonData)

{ “ID” : 210450, “login” : “admin”, “name” : “John Smith”, “password” : “root”, “phone” : 5550505, “email”: “[email protected]”, “online”: true}
The result of the dumps method is passed to a variable called jsonData. Thus, the dictData dictionary was converted into JSON format with just one line. As you can see, thanks to the print function, all information has been encoded in its original form. It’s worth noting that the data from the online field was converted from the literal True to true. Let’s use Python to write the json into a file. To do this, let’s add the code of the previous example as follows:

with open("data.json", "w") as file:

file.write(jsonData)
Read more about writing data to text files in a separate article on our site.

Parsing JSON data

The inverse operation of JSON format decoding with the help of Python language is fulfilled by embedded loads method. With its help you can easily convert JSON into object which you can freely interact with in your program. The example below shows how to create a similar JSON object with user information. Passing the variable jsonData as a parameter to the method loads you can get the dictionary dictData. And from it we can get all necessary data. As you can see from the example, print outputs the individual fields of the dictData, such as full name, phone number, email address and activity information.

import json

jsonData = “”” {
“ID” : 210450,
“login” : “admin”,
{ “name” : “John Smith”,
“password” : “root”,
“phone” : 5550505,
“email” “[email protected]”,
“online” : true
} “””
dictData = json.loads(jsonData)
print(dictData[“name”])
print(dictData[“phone”])
print(dictData[“email”])
print(dictData[“online”])

John Smith
5550505
[email protected]
True
As in the JSON encoding example, the true literal has been automatically converted to True so that it can be handled using Python tools. Note that the dumps and loads functions are capable of interacting with more complex kinds of objects, including nested variants of dictionaries with many string values. This is how JSON data is encoded and decoded using the built-in features of the Python programming language. As you can see, these operations are fairly straightforward, thanks to the handy dumps and loads methods of the json module.

Related Posts

LEAVE A COMMENT