Jolt 将对象列表转换为数组

问题描述

我是 jolt 的新手。我试图实现的转换非常简单。我有一个如下所示的 json

{
  "Object": [
    {
      "date": "01-01-2021","Sold": {
        "ItemType": "New","ItemID": 1,"Description": "desc 1"
      }
    },{
      "date": "01-01-2021","Sold": {
        "ItemType": "New 2","ItemID": 2,"Description": "desc 2"
      }
    }
  ]
}

我的预期输出


[{
        "ItemType": "New",},{

        "ItemType": "New 2",}]

但是当我使用这个变换时

[

  {
    "operation": "shift","spec": {
      "Object": {
        "*": {
          "Sold": {
            "ItemType": "ItemType"
          }
        }
      }
    }
  }
]

我得到的输出

{
  "ItemType" : [ "New","New 2" ]
}

我犯了什么错误

解决方法

您应该阅读有关在 JSON 数组上应用 JOLT 的内容,here for example。 您需要使用 [&2] 引用索引数组,这意味着在三个级别上查找树:零(对象),然后是一(已售出),然后是二(ItemType/ItemID),并将其用作输出中的索引数组。

[
  {
    "operation": "shift","spec": {
      "Object": {
        "*": {
          "Sold": {
            "ItemType": "[&2].ItemType","ItemID": "[&2].ItemID"
          }
        }
      }
    }
  }
]

输出:

[ {
  "ItemType" : "New","ItemID" : 1
},{
  "ItemType" : "New 2","ItemID" : 2
} ]