JMESPath Python: JSON Query Language

JMESPath is a JSON query language. JMESPath in Python allows you to obtain the data you need from a JSON document or dictionary easily. This library is available for Python, but also for many other programming languages, meaning that if you master the JMESPath query language, you can use it in many places.

The problem JMESPath solves

As we’ve seen in the article on JSON, it’s easy to get a nested value from a Python dictionary using Python’s own JSON library. For example: doc["person"]["age"] will get you the nested value for age in a document that looks like this:

{
  "person": {
    "name": "erik",
    "age": 38
  }
}

But what if you want to extract all the age fields from an array of persons, in a document like the one below?

{
  "persons": [
    { "name": "erik", "age": 38 },
    { "name": "john", "age": 45 },
    { "name": "rob", "age": 14 }
  ]
}

We could write a Python for-loop or a list comprehension and loop over all the persons. Easy peasy: [p['age'] for p in doc['persons']]. For a simple case like this one, that’s perfectly fine. But as documents get more deeply nested, and you need to filter and pick values at several levels, the loops and if-statements pile up quickly. This is where JMESPath comes in!

With JMESPath, you describe what you want in a short query, instead of writing code that says how to get it. Because a query is just a string, you can also store it in a configuration file, and the same query works in every language that has a JMESPath library. It’s not faster than a well-written comprehension, but it’s often a lot shorter and easier to read. It’s also the query language of the AWS command-line tool, so you may already have seen it.

Installing JMESPath for Python

JMESPath is not part of the Python standard library, meaning you’ll need to install it. The JMESPath package is hosted on PyPI, like most Python packages. It’s available under the unsurprising name jmespath.

Inside an activated virtual environment, you can install it with pip, start Python, and import the module as follows:

$ pip install jmespath
$ python
>>> import jmespath
>>> j = { "people": [{ "name": "erik", "age": 38 }] }
>>> jmespath.search("people[*].age", j)
[38]Code language: Shell Session (shell)

If you manage your project with uv, run uv add jmespath instead.

JMESPath Python examples

Let’s start with some simple use-cases. First, we define the document with persons from the problem statement as a Python dictionary. Then we fetch the first person from the array, and the first person’s age:

>>> import jmespath
>>> persons = {
...   "persons": [
...     { "name": "erik", "age": 38 },
...     { "name": "john", "age": 45 },
...     { "name": "rob", "age": 14 }
...   ]
... }
>>> jmespath.search('persons[0]', persons)
{'name': 'erik', 'age': 38}
>>> jmespath.search('persons[0].age', persons)
38

In the problem statement above, we wanted to extract all the age fields from the array of persons in the JSON document. This JMESPath expression will get the job done:

>>> jmespath.search('persons[*].age', persons)
[38, 45, 14]

Suppose you want to filter the list, and only get the ages for people named ‘erik’. You can do so with a filter:

>>> jmespath.search("persons[?name=='erik'].age", persons)
[38]

Note that we’re now using double quotes because we need to quote the name inside the filter expression.

Filters can compare numbers too. In JMESPath, a literal number goes between backticks. This gets the names of everyone older than 18:

>>> jmespath.search("persons[?age > `18`].name", persons)
['erik', 'john']

One more nice property: if a key doesn’t exist, JMESPath returns None instead of raising a KeyError. So jmespath.search('company.address.city', persons) simply returns None, which saves you a lot of checks when the data isn’t complete.

Keep learning

You’re now ready to start experimenting! If you want to learn more, try these links:

Related articles

Leave a Comment