AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

JSONPath in Python: Parse JSON Data

PARTAGEZ

Today, JSON is one of the most used formats for exchanging data between applications, especially on the Internet. JSONPath is an expression language for reading specific data from JSON objects. Discover with us the JSONPath implementation developed by Python and its main applications, presented using simple examples.

JSONPath in Python: what is it?

JSON is a file format compatible with many systems, which simplifies and optimizes the exchange of structured data between applications. THE JSON files are composed of key-value pairs (Key-Value Pairs). Values ​​can correspond to different data types, whether primitive values ​​or objects. These objects can also contain their own key-value pairs. Since the JSON format is supported by the vast majority of modern systems, it can be used for data exchange between applications of any type, whether on a computer (locally) or over the Internet.

However, not all of these applications always need all of the data contained in a JSON file. In this type of situation, the JSONPath solution can therefore be useful to you. JSONPath is an expression language allowing targeted reading of specific information from JSON objects. In most programming languages, JSONPath should be imported from an external library. As such libraries need to be implemented separately for each language, these libraries or implementations may be slightly different from each other.

The jsonpath-ng module in Python

jsonpath-ng is certainly the most common implementation of JSONPath in Python. Python has also developed other JSONPath implementations, including jsonpath And jsonpath-rw. However, these remain less popular and complete, so this article focuses exclusively on jsonpath-ng.

Facility

It is very easy to install jsonpath-ng through your shell. To do this, simply enter the order pip install jsonpath-ng.

The installation is carried out from the pip package manager, which Python uses by default. If this package manager is not installed on your system, then you need to start there. For more information on this subject, you can consult the pip website.

Syntax

JSONPath allows you to run complex queries on JSON objects. The module therefore contains several methods, operators and atomic expressions that can help you select and query specific data. The two main methods used by JSONPath are parse() And find(). With parse()you can define queries to reference and repeat them as necessary later. With find()you can run said queries on JSON data, so that extract concrete values. To better understand how they work, study the example below:

import json
import jsonpath_ng as jp
raw_data=""'
{
    "nom": "John",
    "âge": 30,
    "domicile": "Marseille"
}
'''
json_object = json.loads(raw_data)
name_query = jp.parse("$.nom")
result = name_query.find(json_object)

print(result[0].value) # Résultat : John

Python

In the above example, the JSON data in the form of a String was converted to a dictionary object through the use of json.loads. This format is in fact the most efficient for working with Python. At the time of the creation of name_querythe request "$.nom" has been defined (to return the value “name”). Subsequently, it was applied to the JSON object with find(). Finally, the query result was saved in the variable resultbefore being read thanks to result[0].value.

As shown in the example above, it is also appropriate to integrate the Python module json so that Python can read the corresponding JSON data from a String or JSON file. These Strings and files can be converted later using loads() Or load()in a format that can be read by Python.

In addition to returning the requested value, the “find” method also brings other contextual information, for example the path to find the desired value. This information is returned in the form of a list, the searched value corresponding to index 0. You can therefore use result[0].value if you want to display the searched value.

In the example above, the dollar sign (“$”) was used to define the query. It is therefore a atomic expression which makes it possible to refer to theroot object JSON. All atomic operators and expressions are listed in the table below.

Expression/operator Meaning Example Explanation
$ Root object $.marc.âge Allows access to the value of the “age” key of the “marc” object.
. Field of an object $.marc Allows access to “marc”, “marc” corresponding to a field of the root object.
.. Recursive search for a field (sub-object fields are also examined) $.people..âge Allows you to return all occurrences of the “age” field in “people” and its sub-objects.
[x] Element in an array $.people[5] Allows access to the sixth element (corresponding to index 5) of the “people” table.
* Wildcard for a number (usually used in conjunction with for loops) $.people[*] Allows access to a field in “people”. When combined with a for loop, each field would be returned in order.

Besides expressions and operators, some filters also allow you to further specify your search. In the JSONPath implementation developed by Python, these filters can be linked to Python operators. All the symbols that can be associated with these filters are presented, using examples, in the following table.

Symbol Meaning Example Explanation
.[?(filter)] General syntax for filters (parentheses are not required) $.people[?(@.name == “Anne”)] Allows access to people with the first name “Anne”.
@ Object subject to examination (often used in the context of a for loop) $.people[?(@.âge < 50)] Allows access to “people” fields whose value in “age” is less than 50.
<, >, <=, >=, == And != Comparison operators (allowing you to filter certain search results) $.people[@.âge < 50 & @.âge > 20] Allows access to people aged between 20 and 50.
& AND logical $.people[?(@.domicile == ‘Paris’ & @.âge > 40)] Allows access to people over the age of 40 AND who live in Paris.

Do you want to use filters? Choose the module jsonpath_ng.extthen refer to it when calling parse().

Example application for JSONPath in Python

import json
import jsonpath_ng as jp


import json
import jsonpath_ng as jp

# JSON-Daten als String
data = """
{
    "villes": [
        {
            "nom": "Paris",
            "région": "Île-de-France",
            "habitants": 2145000,
            "estunecapitale": true,
            "15ième arrondissement": {
                "habitants": 230000    
            }
        },
        {
            "nom": "Marseille",
            "région": "Provence-Alpes-Côte d'Azur",
            "habitants": 873000,
            "estunecapitale": false
        },
        {
            "nom": "Lyon",
            "région": "Rhône-Alpes",
            "habitants": 537000,
            "estunecapitale": false
        },
        {
            "nom": "Lille",
            "région": "Hauts-de-France",
            "habitants": 236000
        }
    ]
}
"""

# Convertir des données de String en object dictionnaire
json_data = json.loads(data)

# Demande : noms de toutes les villes
query1 = jp.parse("villes[*].nom")
for match in query1.find(json_data):
    print(match.value)     # Résultat : Paris, Marseille, Lyon, Lille

# importer jsonpath_ng.ext pour appliquer les filtres
import jsonpath_ng.ext as jpx

# Demande : noms de toutes les villes de moins de 1 million d'habitants
query2 = jpx.parse("$.villes [?@.habitants < 1000000].nom")
for match in query2.find(json_data):
    print(match.value)     # Résultat : Marseille, Lyon, Lille

# Tous les champs avec "habitants"
query3 = jp.parse("$.villes..habitants")
match = query3.find(json_data)
for i in match:
    print(i.value)     # Résultat : 2145000, 230000, 873000, 537000, 236000

# Les noms de toutes les villes qui ne s'appellent pas "Marseille"
query4 = jpx.parse('$.villes[?(@.nom != "Marseille")].nom')
for match in query4.find(json_data):
    print(match.value)     # Résultat : Paris, Lyon, Lille

Python

In this example, the JSON data takes the form of a String, before being converted into a dictionary object using loads(). The root object contains only an array, itself containing four cities. For each city, there are four fields corresponding to the following data:

  • the name of the city;
  • the region in which it is located;
  • its number of inhabitants;
  • an indication specifying whether or not it is the capital of France.

The city of Paris has an additional field called “15th arrondissement”, to which a number of inhabitants is also associated.

Once the data is converted into a suitable format, four different ones are executed. Their modes of operation and results are detailed directly in the example using comments. As you may have noticed, the third query returns five different values; in fact, the operator .. searches for matching fields recursively. All objects are thus examined, including all of the “children” of said objects. Thus, the number of inhabitants in the district of Pankow appears next to the number of inhabitants in the other towns.

This solution combining JSON and Python constitutes a versatile tool in the field of Internet programming. Do you want to quickly, easily and directly publish a web application using Git? The Deploy Now method offered by IONOS is perfect for you.

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact