Transforming Array of objects with nested array objects using JOLT

寵の児 提交于 2020-01-24 10:22:10

问题


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:


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


来源:https://stackoverflow.com/questions/55827753/transforming-array-of-objects-with-nested-array-objects-using-jolt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!