3
votes

I want to transform following input JSON to output JSON format

INPUT JSON:

[
  {
    "orderNumber": "201904-000000001",
    "items": [
      {
        "itemPrice": 40000,
        "itemQuantity": 11,
        "item": {
          "external_id": "IPHONE"
        }
      },
      {
        "itemPrice": 25000,
        "itemQuantity": 22,
        "item": {
          "external_id": "ONEPLUS"
        }
      },
      {
        "itemPrice": 35000,
        "itemQuantity": 33,
        "item": {
          "external_id": "SAMSUNGS10"
        }
      }
    ]
  }
]

OUTPUT JSON:

[{
  "orderNumber" : "201904-000000001",
  "items" : [ {
    "itemQuantity" : 11,
    "external" : "IPHONE"
  } ]
},
{
  "orderNumber" : "201904-000000001",
  "items" : [ {
    "itemQuantity" : 22,
    "external" : "ONEPLUS"
  } ]
},
{
  "orderNumber" : "201904-000000001",
  "items" : [ {
    "itemQuantity" : 33,
    "external" : "SAMSUNGS10"
  } ]
}]

I have tried following spec which is not working...could someone guide me about spec I should use and explain each step if possible if nested arrays and objects are even deeper how to convert

SPEC I HAVE USED:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "orderNumber": "[&1].orderNumber",
        "items": {
          "*": {
            "itemQuantity": "[&1].items[].itemQuantity",
            "item": {
              "external_id": "[&1].items[].external"
            }
          }
        }
      }
    }
}
]
1

1 Answers

1
votes

Thanks guys, Following spec did work for me after trying different combinations. If anyone comes across this question please explain to me the answer for not trying combinations next time

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "orderNumber": "[&1].orderNumber",
        "items": {
          "*": {
            "itemQuantity": "[&3].items[&1].itemQuantity",
            "item": {
              "external_id": "[&4].items[&2].external"
            }
          }
        }
      }
    }
}
]