【问题标题】:Jolt spec to transform an array of elements into objects with idsJolt 规范将元素数组转换为具有 id 的对象
【发布时间】:2021-04-08 23:13:01
【问题描述】:

Jolt 对我来说是个新手,我一直在努力解决这个问题,直到我创建了这篇文章。

我想转这个:

{
    "Properties": [{
        "Id": "property1",
        "Values": ["randomValue1", "randomValue2"]
    }, {
        "Id": "property2",
        "Values": "randomValue3"
    }, {
        "Id": "property3",
        "Values": "randomValue4"
    }]
}

进入这个

{
    "Properties": [{
        "Id": "property1",
        "Values": "randomValue1"
    },{
        "Id": "property1",
        "Values": "randomValue2"
    }, {
        "Id": "property2",
        "Values": "randomValue3"
    }, {
        "Id": "property3",
        "Values": "randomValue4"
    }]
}

每个属性的值可以是 1 个值,也可以是一个包含未知数量值的数组。

我已经将以下 json 更改为第一个 json 中的内容:

{
    "Properties": {
        "property1": ["randomValue1", "randomValue1"],
        "property2": ["randomValue3"],
        "property3": ["randomValue4"]
    }
}

规格:

[{
    "operation": "shift",
    "spec": {
        "Properties": {
            "*": {
                "*": "Properties[#2].Values",
                "$": "Properties[#2].Id"
            }
        }
    }
}]

RHS 上的属性名称是通用的,属性的值的数量也可能不同。 非常感谢您抽出宝贵时间为我提供帮助。

【问题讨论】:

    标签: json apache-nifi jolt


    【解决方案1】:

    检查是否有帮助:

    [
      {
        "operation": "cardinality",
        "spec": {
          "Properties": {
            "*": {
              // normalize values to always be a list
              "Values": "MANY"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "Properties": {
            "*": {
              "Values": {
                "*": {
                  // create arrays with values and ids
                  "@": "Values",
                  "@(2,Id)": "Id"
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "Values": {
            "*": {
              // create the final list joining ids and values at the indexes you want
              "@": "Properties[&1].Values",
              "@(2,Id[#1])": "Properties[&1].Id"
            }
          }
        }
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 2020-11-14
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多