Enroll in Datacamp's Python Programming Skill Track and learn how to code like a real programmer!
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.
In this article, you will learn how to use JSONPath for Python along with a brief introduction to JSON data.
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.
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.
Enroll in Datacamp's Python Programming Skill Track and learn how to code like a real programmer!
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.
The complete JSONPath Python Syntax for the jsonpath-ng library is as given below:
Syntax | Meaning |
---|---|
$ | The root object |
`this` | The "current" object. |
`foo` | More generally, this syntax allows "named operators" to extend JSONPath is arbitrary ways |
field | Specified field(s), described below |
[ field ] | Same as field |
[ idx ] | Array access, described below (this is always unambiguous with field access) |
Syntax | Meaning |
---|---|
jsonpath1 . jsonpath2 | All nodes matched by jsonpath2 starting at any node matching jsonpath1 |
jsonpath [ whatever ] | Same as jsonpath. whatever |
jsonpath1 .. jsonpath2 | All nodes matched by jsonpath2 that descend from any node matching jsonpath1 |
jsonpath1 where jsonpath2 | Any nodes matching jsonpath1 with a child matching jsonpath2 |
jsonpath1 | jsonpath2 | Any nodes matching the union of jsonpath1 and jsonpath2 |
Syntax | Meaning |
---|---|
fieldname | the field fieldname (from the "current" object) |
"fieldname" | same as above, for allowing special characters in the fieldname |
'fieldname' | ditto |
* | any field |
field , field | either of the named fields (you can always build equivalent jsonpath using | ) |
Syntax | Meaning |
---|---|
[ 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.
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.
Do you want to learn Python, Data Science, and Machine Learning while getting certified? Here are some best selling Datacamp courses that we recommend you enroll in: