JSON Path
KodeKloud
- 3 minutes read - 494 wordsJSON Path is a query language for JSON data structures, originally created by Stefan Goessner and now maintained as the Jayway JsonPath Java library. It works much like SQL does for relational data: you write an expression to select specific elements from a JSON document using paths, indexes, wildcards, and filter criteria. The resources linked below cover everything from beginner syntax to advanced Java API usage and practical kubectl integration. You’ll find this handy in day-to-day work with Kubernetes as well as when you work with other JSON application outputs. If you are looking for an alternative in the command line, you can install and use jq.
Core Syntax at a Glance
| Operator | Meaning | Example |
|---|---|---|
$ | Root element | $.store |
. | Child access | $.store.book |
['key'] | Bracket child access | $['store']['book'] |
[n] | Array index | $.book[0] |
[a:b] | Array slice | $.book[0:2] |
* | Wildcard (all children) | $.store.* |
.. | Deep scan (recursive) | $..author |
[?(expr)] | Filter predicate | $.book[?(@.price < 10)] |
@ | Current item in a filter | @.color == "blue" |
Quick Examples
Given:
{
"store": {
"book": [
{ "title": "Sayings of the Century", "price": 8.95 },
{ "title": "Sword of Honour", "price": 12.99 },
{ "title": "Moby Dick", "price": 8.99 }
],
"bicycle": { "color": "red", "price": 19.95 }
}
}| Query | Returns |
|---|---|
$.store.book[*].title | All book titles |
$..price | Every price in the document |
$.store.book[?(@.price < 10)] | Books under $10 |
$..book[2].title | “Moby Dick” |
$..book.length() | 3 |
What Is in the Resources
Jayway JsonPath (GitHub)
The Jayway JsonPath repository is the Java implementation of the spec. It covers:
- Maven setup and version notes (v3.0.0 requires Java 17)
- Full operator and function reference (
min,max,avg,sum,length,keys,first,last,index, etc.) - Extended filter operators beyond the basics:
=~(regex),subsetof,anyof,noneof,size,empty - Java API for reading, setting, and modifying JSON documents
- Predicate building via inline expressions, the
Filter/CriteriaAPI, or customPredicateimplementations - Configuration options like
DEFAULT_PATH_LEAF_TO_NULL,ALWAYS_RETURN_LIST, andSUPPRESS_EXCEPTIONS - Multiple JSON providers (JsonSmart, Jackson, Gson, Jakarta) and POJO mapping
KodeKloud Course (PDF)
The attached PDF is a beginner-friendly walkthrough that covers:
- YAML vs JSON and why JSON Path targets the JSON form
- Dictionaries, lists, and nested structures with visual examples
- Criteria and filter expressions step by step
- Wildcard usage across objects and arrays
- JSON Path in Kubernetes:
kubectlexamples,rangeloops, custom columns, and sorting
JSON Path in kubectl
This is where JSON Path gets really practical. Instead of dumping raw JSON, you extract exactly what you need.
In kubectl, the $ root is omitted. Queries start with ..
Common Patterns
| Goal | Command |
|---|---|
| All pod names | kubectl get pods -o=jsonpath='{.items[*].metadata.name}' |
| All node CPU counts | kubectl get nodes -o=jsonpath='{.items[*].status.capacity.cpu}' |
| First pod’s image | kubectl get pods -o=jsonpath='{.items[0].spec.containers[0].image}' |
Formatted Output with range
kubectl get nodes -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.capacity.cpu}{"\n"}{end}'master 4
node01 4Custom Columns
kubectl get nodes -o=custom-columns=NODE:.metadata.name,CPU:.status.capacity.cpuNODE CPU
master 4
node01 4Sorting
kubectl get nodes --sort-by=.metadata.name
kubectl get nodes --sort-by=.status.capacity.cpu