Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to project list of lists using JMESPath?

Suppose I have the json:

[
 [0, "a"],
 [1, "b"],
 [2, "c"]
]

How can I create a JMESPath projection to get:

[
  {"id": 0, "name": "a"},
  {"id": 1, "name": "b"},
  {"id": 2, "name": "c"}
]
like image 209
jack Avatar asked Oct 29 '25 17:10

jack


1 Answers

For a pure JMESPath solution — not relaying on any programming language nor on any templating language — use the query:

[*].{id: @[0], name: @[1]}

The @ sign represents the current node, so, in your case, one of the list of list that JMESPath is currently considering.

This would yield your expected output:

[
  {
    "id": 0,
    "name": "a"
  },
  {
    "id": 1,
    "name": "b"
  },
  {
    "id": 2,
    "name": "c"
  }
]

Also, because the current node is implicit in a projection, you can even shorten it to

[*].{id: [0], name: [1]}
like image 114
β.εηοιτ.βε Avatar answered Oct 31 '25 06:10

β.εηοιτ.βε



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!