使用 Jolt 组合两个数组和转换

Combining two arrays and transformation using Jolt

我在使用 Jolt 映射尝试将输入转换为必要形式时遇到困难

输入JSON

{
  "data": [
    {
      "name": "Alcohol",
      "collection_id": 123,
      "properties": [
        {
          "name": "Tax",
          "property_id": "00001"
        },
        {
          "name": "Expenditure",
          "property_id": "00002"
        }
      ],
      "attributes": [
        {
          "name": "alcohol_tax",
          "attribute_id": "00011"
        },
        {
          "name": "alcohol_expenditure",
          "attribute_id": "00022"
        }
      ]
    }
  ]
}

输出JSON

[
    {
        "name": "Alcohol",
        "collection_id": 123,
        "details": [{
                "property_name": "Tax",
                "property_id": "00001",
                "attribute_id": "00011"
            },
            {
                "property_name": "Expenditure",
                "property_id": "00002",
                "attribute_id": "00022"
            }
        ]
    }
]

我尝试了几种使用一些规则组合数组的方法,但收效甚微。

规则之一

[{
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "name": "&1.name",
          "collection_id": "&1.collection_id",
          "attributes": {
            "*": {
              "attribute_id": "&1.attribute_id[]"
            }
          },
          "properties": {
            "*": {
              "name": "&1.myname[]",
              "property_id": "&1.property_id[]"
            }
          }
        }
      }
    }
}]

正在将所有属性和属性添加到所有 collections.I 不知道为什么会发生这种情况,因为我认为 &1.property_id[] 只会添加特定 collection 中的项目数组而不是所有 collections。任何关于为什么会发生这种情况的 help/clues 都将不胜感激。

参见下面的解决方案:

  • [0] 创建环绕数组
  • [&1] 使用各个数组的位置,因此结果合并在 details 中,重要的部分是包装方括号,因此它被视为数组而不是文字。
[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "name": "[0].name",
          "collection_id": "[0].collection_id",
          "attributes": {
            "*": {
              "attribute_id": "[0].details.[&1].attribute_id"
            }
          },
          "properties": {
            "*": {
              "name": "[0].details.[&1].name",
              "property_id": "[0].details.[&1].property_id"
            }
          }
        }
      }
    }
  }
]

生成以下内容:

[
  {
    "name": "Alcohol",
    "collection_id": 123,
    "details": [
      {
        "attribute_id": "00011",
        "name": "Tax",
        "property_id": "00001"
      },
      {
        "attribute_id": "00022",
        "name": "Expenditure",
        "property_id": "00002"
      }
    ]
  }
]