震动规格将元素数组转换为具有ID的对象

问题描述

震动对我来说是陌生的,直到我创建此帖子为止,我一直在努力解决这个问题。

我要转这个:

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

进入

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

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

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

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

规格:

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

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

解决方法

检查这是否有帮助:

[
  {
    "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"
            }
          }
        }
      }
    }
  },"spec": {
      "Values": {
        "*": {
          // create the final list joining ids and values at the indexes you want
          "@": "Properties[&1].Values",Id[#1])": "Properties[&1].Id"
        }
      }
    }
  }
]