Tutorial

Python JSONPath Examples

Published on August 3, 2022
author

Pankaj

Python JSONPath Examples

What is JSONPath?

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.

  1. jsonpath: It’s a port of the Perl, and JavaScript versions of JSONPath.
  2. 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.
  3. 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.

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.

$ pip3.7 install jsonpath-ng
Python Jsonpath Ng Install
Python jsonpath-ng Install

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.

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:

[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.

{
  "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.

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:

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

Recommended Read: Python f-strings – PEP 498 – Literal String Interpolation

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

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 few of the values.

References

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
November 25, 2021

Hi Pankaj, Given the JSON structure as below is there a way using Python we can find if a given node has child nodes or not { “class”: { “student”: { “name”: “James”, “marks”: { “physics”: 70, “chemistry”: 73, “mathematics”: 80 } } } }

- Shubhojit

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    March 17, 2020

    Hi Pankaj, Thanks for your nice introductory tutorial on search/parsing a json object. How would you go about extracting a specific set of k, v pair, - given the key. Note that there will be multiple occurrences of the key, with different values. For example, I want to extract all the ‘Statement: ’ in this roles.json (https://github.com/HumanCellAtlas/data-store/blob/a35044e5002b3d05a729702f08ccafeb9aaf1a5f/roles.json). a = datums = parse(’$…Statement’).find(a) len(datums) = 5. ===> this is not what I want. In fact, there are many more statements in roles.json Your help is very much appreciated.

    - Sri

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      February 7, 2020

      What if I only want to read info for employee 1, not for employee 2?

      - Jie Liu

        Try DigitalOcean for free

        Click below to sign up and get $200 of credit to try our products over 60 days!

        Sign up

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Become a contributor for community

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        DigitalOcean Documentation

        Full documentation for every DigitalOcean product.

        Resources for startups and SMBs

        The Wave has everything you need to know about building a business, from raising funding to marketing your product.

        Get our newsletter

        Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

        New accounts only. By submitting your email you agree to our Privacy Policy

        The developer cloud

        Scale up as you grow — whether you're running one virtual machine or ten thousand.

        Get started for free

        Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

        *This promotional offer applies to new accounts only.