JMESPath for JSON Processing: A Quick Start Guide

If you have used the AWS CLI with --query, you have already used JMESPath. It is the query language behind AWS CLI, Ansible, Azure CLI, and a growing number of DevOps tools.

Unlike JSONPath (which focuses on navigation), JMESPath focuses on transformation — it can filter, sort, project, and combine data in ways JSONPath cannot.

Basic Expressions

{
  "name": "Alice",
  "age": 30,
  "address": {
    "city": "New York",
    "zip": "10001"
  }
}
Expression: name
Result: "Alice"

Expression: address.city
Result: "New York"

Expression: address
Result: { "city": "New York", "zip": "10001" }

Array Operations

{
  "people": [
    { "name": "Alice", "age": 30, "active": true },
    { "name": "Bob", "age": 25, "active": false },
    { "name": "Charlie", "age": 35, "active": true }
  ]
}
// All names
people[*].name
→ ["Alice", "Bob", "Charlie"]

// First person
people[0]
→ { "name": "Alice", ... }

// Slice: second and third
people[1:3]
→ [Bob, Charlie]

Filtering (Where JMESPath Shines)

JMESPath filter expressions are cleaner than JSONPath:

// Active users only
people[?active == `true`]
→ [Alice, Charlie]

// Users over 30
people[?age > `30`]
→ [Charlie]

// Active users older than 28
people[?active == `true` && age > `28`]
→ [Alice, Charlie]

Note the backtick syntax around literal values (true, numbers, strings). This is JMESPath's way of distinguishing literals from field names.

Projections

JMESPath can reshape JSON output:

// Get specific fields as objects
people[*].{name: name, age: age}
→ [
    { "name": "Alice", "age": 30 },
    { "name": "Bob", "age": 25 },
    { "name": "Charlie", "age": 35 }
  ]

// Flatten nested arrays
people[*].name | sort(@)
→ ["Alice", "Bob", "Charlie"]

The pipe | operator passes the result of the left side to the function on the right side.

Built-in Functions

JMESPath has functions that JSONPath does not:

# Sorting
aws ec2 describe-instances --query 'sort_by(Reservations[*].Instances[*], &LaunchTime)[].[InstanceId,LaunchTime]'

# Max/Min
people | max_by(@, &age).name
→ "Charlie"

# Length
people | length(@)
→ 3

# Contains
people[?contains(name, 'li')]
→ [Alice]

# Join
people[*].name | join(', ', @)
→ "Alice, Bob, Charlie"

Real DevOps Example

# Get the most expensive EC2 instance
aws ec2 describe-instances --region us-east-1 \
  --query 'Reservations[*].Instances[*] | [] | sort_by(@, &InstanceType) | [-1] | {InstanceId: InstanceId, InstanceType: InstanceType, LaunchTime: LaunchTime}'

On Stack Overflow, the JMESPath vs JSONPath comparison has 22,000 views. The consensus: JSONPath for quick data extraction, JMESPath for complex data processing.

Related Searches

  • jmespath tutorial
  • jmespath filter expression
  • jmespath built-in functions
  • jmespath vs jsonpath
  • jmespath aws cli examples
  • jmespath sort by
  • jmespath pipe operator
  • jmespath projection
  • jmespath array operations
  • jmespath online tester

Frequently Asked Questions

What is the difference between JMESPath and JSONPath?

JSONPath focuses on navigating JSON structures (like XPath for XML). JMESPath focuses on transforming JSON data and has built-in functions for sorting, filtering, and aggregating. JSONPath is better for simple extraction. JMESPath is better for complex queries and transformations.

Is JMESPath supported in programming languages?

Yes. JMESPath has official libraries for Python, JavaScript, Go, Java, Ruby, PHP, and Rust. The Python library (jmespath) and JavaScript library (jmespath) are the most widely used.

What is the [] flatten operator in JMESPath?

[] flattens nested arrays into a single array. Reservations[*].Instances[*] returns an array of arrays. Adding [] at the end flattens it: Reservations[*].Instances[*][] returns all instances in a single array. This is commonly used with AWS CLI queries.

Can JMESPath handle null values?

Yes. Use the || operator to provide defaults: field || 'default'. Use ?? to check for existence: field ?? 'not_found'. JMESPath's null handling is more explicit than JSONPath's.

Final Thoughts

JMESPath is the right tool when you need to transform JSON data — not just extract it. Its built-in functions, pipe operator, and projection syntax make it ideal for DevOps automation, log processing, and any scenario where raw JSON needs reshaping before consumption.