JSONPath Online logojsonpath.online

JMESPath for Python

Learn the jmespath Python library with ready-to-use snippets. Search JSON payloads, optimize with compiled expressions, and export to CSV with pandas.

Quick start

  1. Install: pip install jmespath
  2. Import and search: jmespath.search(expression, data)
  3. Compile for loops: expr = jmespath.compile("..."); expr.search(data)

Examples you can copy

Basic search

import json, jmespath

data = json.loads('{"people":[{"name":"Ana","age":32},{"name":"Ben","age":27}]}')
print(jmespath.search("people[?age > `30`].name", data))

Compiled expression (faster)

import jmespath

expr = jmespath.compile("items[].{id:id, price:price}")
for payload in payloads:
    rows = expr.search(payload)
    process(rows)

Error handling

import json, jmespath
try:
    data = json.loads(bad_json)
    result = jmespath.search("orders[].id", data)
except json.JSONDecodeError as e:
    print("Invalid JSON", e)
except jmespath.exceptions.JMESPathError as e:
    print("Invalid expression", e)

Flatten nested arrays

import jmespath, json

data = json.loads(open("orders.json").read())
expr = "orders[].items[].{orderId: orderId, sku: sku, qty: quantity}"
rows = jmespath.search(expr, data)

With pandas CSV export

import json, jmespath, pandas as pd

data = json.loads(open("input.json").read())
rows = jmespath.search("people[].{Name:name, City:location.city}", data)
df = pd.json_normalize(rows)
df.to_csv("people.csv", index=False, encoding="utf-8-sig")

Custom function (uppercase)

import jmespath

class UpperRuntime(jmespath.functions.Functions):
    @jmespath.functions.signature({'types': ['string']})
    def _func_upper(self, s):
        return s.upper()

options = jmespath.Options(custom_functions=UpperRuntime())
print(jmespath.search("upper('hello')", {"dummy": 1}, options=options))

Patterns with AWS SDK (boto3)

  • Filter EC2 responses: jmespath.search("Reservations[].Instances[].InstanceId", resp)
  • Summaries for dashboards: jmespath.search("Functions[].{name:FunctionName,runtime:Runtime}", resp)
  • Combine with paginator loops; compile the expression once and reuse.

CSV with pandas

Use JMESPath to normalize, then pandas to export. This is ideal for "jmespath create csv" workflows in CI.

rows = jmespath.search("Reservations[].Instances[].{Id:InstanceId,Type:InstanceType}", data)
df = pd.json_normalize(rows)
df.to_csv("instances.csv", index=False, encoding="utf-8-sig")

Debugging tips

  • Validate JSON before search: wrap loads in try/except.
  • Start simple: search("[0]", data) to confirm shape.
  • Log intermediate results: split expressions into smaller parts.
  • Use the online tester to iterate quickly, then copy the final expression into Python.

FAQ

How do I install JMESPath for Python?

Run pip install jmespath. It is a small pure-Python library.

How do I compile an expression?

Use jmespath.compile('expression'), then call .search(data) repeatedly for better performance.

Can I combine JMESPath with pandas?

Yes. Search first, then pass the result into pandas.json_normalize and export to CSV.

How do I handle missing keys?

Use default values or filters: expr = "items[].{name: name || 'unknown'}".

Is JMESPath part of boto3?

boto3 uses JMESPath internally for resources, but you can still import jmespath directly for custom searches.

Does it mutate my data?

No. jmespath.search returns a new object and does not change the original data structure.