DEV Community

Ilya R
Ilya R

Posted on

JSON in Python3

JSON (JavaScript Object Notation) is a text-based data exchange between different services.

In Python 3, as in many other programming languages, the JSON format is widely used in web development for messaging between a client and a server or various microservices. Also, it is not rare that system settings are stored in JSON format, which are placed in special files with the .json extension. In Python 3, a JSON object is obtained from a dictionary. Typically, a dictionary is serialized, i.e. cast to a JSON string. And such a string is sent, for example, in an HTTP request to a remote server, to a client, or to another microservice. Python 3 has built-in tools for working with the JSON format, which will be described later in the text. When you need to send several dictionaries (JSON objects) to the same remote server or microservice, the dictionaries are placed in a list and this list is also converted using Python 3 to a JSON string.

Let's take a little look at the history of the creation of the JSON format and what it was created for. The development of the JSON format began in 2001 by Chip Moningstar and Douglas Crockford. Douglas and Chip were looking for a way to transfer information between a browser and a server more efficiently than XML. It needed a way to exchange data between programs written in two different languages. The model for working with XML is that a request is first sent to the server, which responds with an XML document. And then, in order to get the data, additional requests were made to the previously received XML document. It's been long enough. A more easy-to-use messaging (data) exchange format was needed.

As a result, the JSON data transfer format was created, which makes it easier to “communicate” between different programs. The format was developed based on the JavaScript language, but in the future this format received complete independence from JavaScript and can be used by almost any programming language.

Initially, the JSON format was intended solely for exchanging data over the network between programs written in different languages. There were a number of difficulties in developing the format. For example, the creators of the format have been trying for a long time to develop a standard for comments in JSON. As a result, they decided to drop it. Comments in JSON are always ignored. But, the creators of the format managed to make JSON able to provide data exchange between languages with different representations of numbers. Different programming languages may have different representations of numbers. For example, in JavaScript there is no difference between floating point numbers or integers. At the same time, in Python 3, floats and integers are different data types. But with the JSON format you don't have to worry about that. You do not need to think in what language this or that microservice with which you want to work is written. All you need to do is create a dictionary, convert it to a JSON string using the built-in tools, and send this string as a request to the microservice you need.

Today, the JSON format does not have a trademark, logo, or copyright. This format is absolutely free and you can freely use it in any projects.

Also, the JSON format does not have any version. This was done so that there would be a single data transfer format.

The structure of JSON objects is quite simple. Such an object is an unordered set of type "key: value". The object itself is enclosed in curly braces. Keys are specified in double quotes. In a JSON object, all strings must be enclosed in double quotes, but not numbers. The key must always be a string. The key:value pairs are separated by commas.

{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
Enter fullscreen mode Exit fullscreen mode

The value may be not only a string or a number, but it can also be another JSON object or an array of such objects. Nesting is not limited to any limit. Inside the array can be all valid values in JSON format. JSON has its own MIME type, application/json.

Types of values in JSON:

  • string
  • number
  • JSON object (not to be confused with objects in Python 3)
  • array
  • boolean value
  • null

It is not uncommon when more then one JSON object need to be passed at once in a list.

[
    {
      "userId": 1,
      "id": 1,
      "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
      "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
    },
    {
      "userId": 2,
      "id": 3,
      "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
      "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
    }
]
Enter fullscreen mode Exit fullscreen mode

Let's see how we can work with JSON format in Python 3.

A JSON object in Python 3 is a dictionary (dict). To send such a dictionary or list of dictionaries from one service to another service or to a remote server, you must first convert the dictionary or list to a valid JSON string. In Python 3, this can be done through the built-in json library. The “dumps” method creates a string from a dictionary or list. And with the help of the “loads” method, the reverse transformation occurs.

# First we include the json library in our file
import json

# Next, we convert the dictionary to a JSON string using the "dumps" method
# A dictionary is passed to the "dumps" method, which will be converted to a JSON string
data = json.dumps({"c": 0, "b": 0, "a": 0})

# And here it is shown how to translate JSON string back into a dictionary
# A JSON string is passed to the "loads" method, which will be converted to a dictionary
dict_from_json = json.loads(data) 
Enter fullscreen mode Exit fullscreen mode

And after transformation list or dict to JSON string you can send that string to remote server, another service or module through HTTP query.

Also JSON data can be in special files with .json extension. Commonly in that are stored system configuration of application. It's comfortable to use because JSON has standard structure and it is easy to transform or read by Python’s methods.

In such files can not only JSON objects, but also there can be a list of JSON objects. Python3 allow as work with such files - read and write. Note that for read data from file you need to use “load” method, but to transform JSON string to dict or list you need to use methods “loads”.

# Connecting json library
import json

# With "open" function we can open file and put it to descriptor f
f = open('data.json')

# Using the "load" method of the "json" library, we will convert the data from the file into a dictionary
data = json.load(f)
print(data)
Enter fullscreen mode Exit fullscreen mode

As you can see from the code example, the “load” method from the json library is used to convert a JSON object into a dictionary. First, the json file is opened, and then the data is converted.

And thus, we can work with the data from the JSON file as with a normal dictionary.

Let's take a look at how this works in practice. For practice we need to get data from a remote server. For this we will use the free JSONPlaceholder API. This API allows us to get fake data in JSON format for learning how to work with the format or for some other non-commercial purposes. You can find documentation on working with this API on their website https://jsonplaceholder.typicode.com

If we want to get data from the API, we need to make a POST request to the URL https://jsonplaceholder.typicode.com/posts . We will do it using the Requests library. The Requests library allows us to easily make HTTP requests and process the received responses. First, let's install this library into the project:

pip install requests

Next, we need to connect the library and execute the request using the Post method.

# Connecting "requests" and "json" libraries
import requests
import json

# Using the get method of the requests library, we make an HTTP request
# to remote server for fetching data
# Documentation shows that the data is transmitted in JSON format
# We put the response from the server in the "res" variable 
res = requests.get('https://jsonplaceholder.typicode.com/posts/1')

# After through the "text" method we get a JSON string from the server response
text = res.text

# To convert a JSON string into a list with which we can already work,
# we need to use the "loads" method of the "json" library
print(json.loads(text))
Enter fullscreen mode Exit fullscreen mode

Thus, we receive data from a remote server as a JSON string, translate this string into a Python 3 list, and after that we can work with data from the server as with a regular list.

From this article, we learned what the JSON format is, why it was created, in what areas it is used, and also told how it can be used in Python 3.

Top comments (0)