Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transforming Array of objects with nested array objects using JOLT

Tags:

java

json

jolt

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"
            }
          }
        }
      }
    }
}
]
like image 304
Pokuri Avatar asked Mar 06 '26 12:03

Pokuri


1 Answers

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"
            }
          }
        }
      }
    }
}
]
like image 68
Pokuri Avatar answered Mar 09 '26 03:03

Pokuri



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!