Search code examples
pythonjsonjinja2jmespath

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"}
]

Solution

  • 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]}