JMESPath (pronounced “James path”) is a query language for JSON. It allows you to declaratively specify how to extract elements from a JSON document.
Unlike jq, which is a standalone CLI tool, JMESPath is often used as a library embedded in other languages (Python, JavaScript, Go, etc.) or tools (AWS CLI).
Key Features
- Declarative: Describe what you want, not how to get it.
- Portable: Implementations available in many languages.
Examples
Given the following JSON:
{
"locations": [
{"name": "Seattle", "state": "WA"},
{"name": "New York", "state": "NY"},
{"name": "Bellevue", "state": "WA"},
{"name": "Olympia", "state": "WA"}
]
}
Basic Expressions
- Select list:
locations - Filter list:
locations[?state == 'WA']- Result:
[{"name": "Seattle", "state": "WA"}, ...]
- Result:
- Projection (Map):
locations[*].name- Result:
["Seattle", "New York", "Bellevue", "Olympia"]
- Result:
- Filter & Project:
locations[?state == 'WA'].name- Result:
["Seattle", "Bellevue", "Olympia"]
- Result:
Python Usage
import jmespath
data = {"foo": {"bar": "baz"}}
result = jmespath.search('foo.bar', data)
print(result) # "baz"
RTFM
- JMESPath Tutorial - The best place to start.
- JMESPath Specification
- parsel - A library that wraps JMESPath and XPath.
