Home > Mobile >  Is there any other way to club the multiple maps in a single list in jolt?
Is there any other way to club the multiple maps in a single list in jolt?

Time:12-06

I have a use case for which I need to club the maps in a single list. But don't want to use another shift operation.

Can anyone help me out?

Input JSON

{
  "orders": [
    {
      "orderId": "10001",
      "order_parts": [
        {
          "id": "00001",
          "items": [
            {
              "quantity": 1,
              "price": 20
            },
            {
              "quantity": 1,
              "price": 20
            }
          ]
        },
        {
          "id": "00004",
          "items": [
            {
              "quantity": 1,
              "price": 20
            }
          ]
        }
      ]
    }
  ]
}

Expected output

{
  "orderId": "10001",
  "order_parts": [
    {
      "id": "00001",
      "items": [
        {
          "quantity": 1,
          "price": 20
        },
        {
          "quantity": 1,
          "price": 20
        }
      ]
    },
    {
      "id": "00004",
      "items": [
        {
          "quantity": 1,
          "price": 20
        }
      ]
    }
  ],
  "totalQuantity": [ 1, 1, 1 ]
}

Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "orders": {
        "*": {
          "order_parts": {
            "*": {
              "items": {
                "*": {
                  "quantity": "[&1].totalQuantity"
                }
              }
            }
          },
          "@": "[&]"// By this I want to copy input json to be used in the next shift.
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&"
      }
    }
  }
]

For now, I have used another shift, but it'll help me if this is not necessary to use another shift operation.

Any help will be appreciated.

Thanks.

CodePudding user response:

You can use the trick of "@": "" instead such as

[
  {
    "operation": "shift",
    "spec": {
      "orders": {
        "*": {
          "order_parts": {
            "*": {
              "items": {
                "*": {
                  "q*": "totalQ&(0,1)"// the replacement of the zero (current) level literal "uantity" for the first asterisk(which might me multiple)
                }
              }
            }
          },
          "@": ""
        }
      }
    }
  }
]
  • Related