SEANK.H.LIAO

extracting from json

extracting partial elements from json

json

You have a JSON shaped problem, and now you want to write a short query for getting something out of it.

 1{
 2  "a": [
 3    {
 4      "x": 100
 5    },
 6    {
 7      "x": 200
 8    }
 9  ]
10}

json pointer

RFC 6901 introduces JSON Pointer: tldr: / separated key names, arrays are 0 indexed. No special syntax for all array elements etc...

# 200
/a/1/x

Not very flexible, but it is used in JSON Patch: RFC 6902

json path

JSON Path is basically XPath for JSON.

tldr: . separated, .. recursive descent, [::] array access, ?() script filter (eg on child attributes)

# 100, 200
$..x
$.a[:].x

# [{ "x": 100 }]
$.a[?(@.x == 100)]

JMESPath

JMESPath

tldr: . separated, no recursive descent(?), [::] array access, | pipe and use functions.

# 100, 200
a[*].x

# { "x": 100 }
a[?x == 100]

jq

jq is a more general purpose utility that can do advanced queries and reshape data as necessary. tldr: . separated, .. recursive descent, [] array access, | pipe and use functions for more.

# 100, 200
..x
.a[].x

# { "x": 100 }
.a[] | select(.x == 100)

cel

cel (spec) is a minimal language for evaluating single expressions. With macros it becomes viable to query nested data structures.

# 100, 200
o.a.map(n, n.x)

# { "x": 100 }
o.a.filter(n, n.x == 100)