Featured image of post JEMSPath

JEMSPath

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"}, ...]
  • Projection (Map): locations[*].name
    • Result: ["Seattle", "New York", "Bellevue", "Olympia"]
  • Filter & Project: locations[?state == 'WA'].name
    • Result: ["Seattle", "Bellevue", "Olympia"]

Python Usage

import jmespath

data = {"foo": {"bar": "baz"}}
result = jmespath.search('foo.bar', data)
print(result) # "baz"

RTFM

使用 Hugo 构建
主题 StackJimmy 设计