Tutorials  /  Python

Python JSONPath Examples

Ccentron Redaktion · February 2024 ·3 min read ·Python, Tutorial

JSONPath is an expression language to parse JSON data. It’s very similar to the XPath expression language to parse XML data. The idea is to parse the JSON data and get the value you want. This is more memory efficient because we don’t need to read the complete JSON data.

Python JSONPath Libraries

There are many JSONPath libraries in Python.

  • jsonpath: It’s a port of the Perl, and JavaScript versions of JSONPath.
  • jsonpath-rw: The complete Python implementation of the JSONPath expressions. The JSONPath expressions are first class objects, easy to analyze, transform, parse, print, and extend. The jsonpath-rw-ext module provides some additional extensions to extend its functionalities.
  • jsonpath-ng: The final implementation of the JSONPath that aims to be standard compliant, including arithmetic and binary comparison operators. This library merges jsonpath-rw and jsonpath-rw-ext modules and further enhances it.
VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Which Python JSONPath Library to Use?

The jsonpath-ng module is the most comprehensive and written purely in Python. It supports both Python 2 and Python 3. So, we will use this module for Python JSONPath examples.

Installing jsonpath-ng Module

We can install jsonpath-ng module using PIP.

Console
$ pip3.7 install jsonpath-ng

Parsing a Simple JSON Data using JSONPath

Let’s look at a simple example to parse the JSON data and get the required attribute value.

Python
import json
from jsonpath_ng import jsonpath, parse
json_string = '{"id":1, "name":"Pankaj"}'
json_data = json.loads(json_string)
jsonpath_expression = parse('$.id')
match = jsonpath_expression.find(json_data)
print(match)
print("id value is", match[0].value)

Output:

Code
[DatumInContext(value=1, path=Fields('id'), context=DatumInContext(value={'id': 1, 'name': 'Pankaj'}, path=Root(), context=None))]
id value is 1

We are using json module to convert the JSON string to a dictionary.

Parsing a List using JSONPath Expression

The JSON key can contain a list of values. We can use JSONPath expression to parse the list and get the list of values. Let’s say we have a JSON file “db.json” with the following contents.

JSON
{"employees": [
{
"id": 1,
"name": "Pankaj",
"salary": "10000"
},
{
"name": "David",
"salary": "5000",
"id": 2
       }
   ]
}

We want to parse this JSON file and get the list of employee ids. We can use JSONPath expressions to get this data very easily.

Python
import json
from jsonpath_ng import jsonpath, parse
with open("db.json", 'r') as json_file:
json_data = json.load(json_file)
print(json_data)
jsonpath_expression = parse('employees[*].id')
for match in jsonpath_expression.find(json_data):
print(f'Employee id: {match.value}')

Output:

Code
{'employees': [{'id': 1, 'name': 'Pankaj', 'salary': '10000'}, {'name': 'David', 'salary': '5000', 'id': 2}]}
Employee id: 1
Employee id: 2

If you want to get the data into a list, you can use Python list comprehension.

Python
emp_ids_list = [match.value for match in jsonpath_expression.find(json_data)]
print(emp_ids_list) # [1, 2]

Conclusion

JSONPath provides us an easy way to parse the JSON data and extract specific values. It’s very useful when the JSON data is huge and we are interested in only a few of the values.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren