0

I have this Json example:

{
    "client_id": 15,
    "orders": [
      {
        "order": 110,
        "status": "APPROVED",
      },
      {
        "order": 141,
        "status": "REJECTED",
      }
    ]
  }

I want to generate the result

[
    {
         "client_id": 15, 
         "order": 110, 
         "status": "APPROVED"
    }, 
    {
         "client_id": 15, 
         "order": 141, 
         "status": "REJECTED"
    }
]

the current sentence [*].map(&merge({client_id: client_id}, @), orders) returns:

[
    {
         "client_id": null, 
         "order": 110, 
         "status": "APPROVED"
    }, 
    {
         "client_id": null, 
         "order": 141, 
         "status": "REJECTED"
    }
]

Why orders values works on second map parameter but client_id not work? And how i can resolve to get the result that i need?

  • You are feeding a function in what `map` expects to be an expression `array[any] map(expression->any->any expr, array[any] elements)`, which cannot work. Having the possibility to query the parent node is not something that exists In JMESPath. See https://stackoverflow.com/questions/52683015/how-to-get-list-of-all-child-elements-with-field-from-parent – β.εηοιτ.βε May 07 '22 at 18:44

1 Answers1

0

This can be done using the comminuty edition of JMESPath that has some additional functionallity. You can use the let function from there: https://jmespath.site/main/#spec-let-expressions

Specifically the below:

let $client_id = client_id in orders | map(&merge({client_id: $client_id}, {order: order, status: status}), @)

Outputs:

[
  { "client_id": 15, "order": 110, "status": "APPROVED" },
  { "client_id": 15, "order": 141, "status": "REJECTED" }
]
Henry Munro
  • 229
  • 2
  • 9