Thanks to theidioms.com

JSONPath Python – Examples and Usage in Python

JSONPath Python - Examples and Usage in Python
Data Science

JSONPath Python – Examples and Usage in Python

JSONPath Python - Examples and Usage in PythonJSONPath Python - Examples and Usage in Python

JSONPath Python is a way to parse JSON data through the use of pre-defined syntaxes in Python. It provides an extension to how you can work with JSON data in Python with fewer lines of code and less memory usage.

JSONPath Python - Examples and Usage in Python

In this article, you will learn how to use JSONPath for Python along with a brief introduction to JSON data.

What is JSON?

If you are unfamiliar with JSON data in general, JSON stands for JavaScript Object Notation and is an open standard file format and data interchange format. Here’s an example of JSON data:

{

  "age": 27,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    }
  ],
  "children": [],
  "spouse": null
}

As you can see, working with such data in Python can be hard since there are multiple braces and brackets to work with. By using JSONPath for Python, you are able to parse such JSON data easily.

Let us learn how to get started with JSONPath for Python.



Getting started with JSONPath for Python

To get started with JSONPath for Python, you can install the jsonpath-ng library using Python’s Package Manager (pip).

The jsonpath-ng library is a standard-compliant implementation of JSONPath for Python. This library differs from other JSONPath implementations in that it is a full language implementation, meaning the JSONPath expressions are first-class objects, easy to analyze, transform, parse, print, and extend.

To install the jsonpath-ng library, open up your command line/terminal and run the following command:

pip install jsonpath-ng

This will install the latest version of the jsonpath-ng library on your machine. You can then import it in Python using a Python IDE or Python Shell by writing the following line of code:

import jsonpath_ng

If running this line of code doesn’t give an error, then, you’ve successfully installed and imported jsonpath-ng in Python. Note that the jsonpath-ng library is written as jsonpath_ng in Python.



JSONPath Python Example

Here’s an example of how JSONPath can be used in Python to work with JSON data:

# Importing jsonpath-ng library in Python
import jsonpath_ng

# Initializing JSON data
json_data = {'foo': [{'baz': 1}, {'baz': 2}]}

# Setting up a parser
jsonpath_expr = jsonpath_ng.parse('foo[*].baz')

# Parsing the values of JSON data
list_val = [match.value for match in jsonpath_expr.find(json_data)]

# Printing the parsed values
print(list_val)
[1, 2]

Here, we first set up a JSON parser by using the parse() method from the jsonpath-ng library. This parse method defines what we want to parse which is in this case the ‘baz’ keys.

Then, using the find() method, we find all of the matches in the JSON data and extract the value of each match.

You can also perform other kinds of operations using JSONPath for Python:

# Importing jsonpath-ng library in Python
import jsonpath_ng

# Automatically providing ids for bits of data that do not have them
jsonpath_ng.jsonpath.auto_id_field = "id"
print(
    [
        match.value
        for match in jsonpath_ng.parse("foo[*].id").find(
            {"foo": [{"id": "bizzle"}, {"baz": 3}]}
        )
    ]
)

# A handy extension: named operators like `parent`
print(
    [
        match.value
        for match in jsonpath_ng.parse("a.*.b.`parent`.c").find(
            {"a": {"x": {"b": 1, "c": "number one"}, "y": {"b": 2, "c": "number two"}}}
        )
    ]
)
['foo.bizzle', 'foo.[1]'] 
['number one', 'number two']

The first print statement in the above JSONPath Python example provides ids for bits of data that do not have the given parse ‘key’ value, that is, ‘id’.

The second print statement in the above JSONPath Python example performs nested data extraction using the parent keyword.



JSONPath Python Syntax – Complete List

The complete JSONPath Python Syntax for the jsonpath-ng library is as given below:

  • Atomic expressions:
SyntaxMeaning
$The root object
`this`The “current” object.
`foo`More generally, this syntax allows “named operators” to extend JSONPath is arbitrary ways
fieldSpecified field(s), described below
[ field ]Same as field
[ idx ]Array access, described below (this is always unambiguous with field access)
  • JSONPath operators:
SyntaxMeaning
jsonpath1 . jsonpath2All nodes matched by jsonpath2 starting at any node matching jsonpath1
jsonpath [ whatever ]Same as jsonpath.whatever
jsonpath1 .. jsonpath2All nodes matched by jsonpath2 that descend from any node matching jsonpath1
jsonpath1 where jsonpath2Any nodes matching jsonpath1 with a child matching jsonpath2
jsonpath1 | jsonpath2Any nodes matching the union of jsonpath1 and jsonpath2
  • Field specifiers:
SyntaxMeaning
fieldnamethe field fieldname (from the “current” object)
"fieldname"same as above, for allowing special characters in the fieldname
'fieldname'ditto
*any field
field , fieldeither of the named fields (you can always build equivalent jsonpath using |)
  • Array specifiers:
SyntaxMeaning
[n]array index (may be comma-separated list)
[start?:end?]array slicing (note that step is unimplemented only due to lack of need thus far)
[*]any array index

You can learn more about the jsonpath-ng library by visiting the library’s GitHub repository.

In Conclusion

You now know how to use JSONPath for Python. If you have any questions, please feel free to comment them down below and we will get back to you.


Are you interested in working with data? Get a free trial month of LinkedIn Learning and learn Data Science and Machine Learning courses using Python, R, and SQL from top instructors.

Enroll in any one of the courses from the following fields today on LinkedIn Learning and start your free month of learning:

Disclaimer: When you subscribe to LinkedIn Learning, we may earn a small commission as an affiliate. We love the platform so much so that we have been using it ourselves at The Click Reader.

JSONPath Python - Examples and Usage in PythonJSONPath Python - Examples and Usage in Python

Leave your thought here

Your email address will not be published. Required fields are marked *