json Jolt -从可以具有字符串或对象值的相同键进行变换

disbfnqx  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(131)

我试图转换一个输入数据,其中相同的键值可以是字符串或对象。但却无法找到正确的转换方式。有人能帮帮我吗。谢谢!
输入

[
  {
    "id": "id1",
    "location": {
      "value": "loc-123"
    }
  },
  {
    "id": "id2",
    "location": {
      "value": "loc-789"
    }
  },
  {
    "id": "id3",
    "location": "loc-666"
  }
]

所需输出:

[
  {
    "id": "id1",
    "loc": "loc-123"
  },
  {
    "id": "id2",
    "loc": "loc-789"
  },
  {
    "id": "id3",
    "loc": "loc-666"
  }
]
neskvpey

neskvpey1#

您可以使用以下转换

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&", // generates array-wise([ ]) results after going one (&1) level up 
                       // the tree to reach the level of indexes within the array
        "*ation": {
          "value": "[&2].&(1,1)",// increments +1 more level compared to the upper one
          "*": { // else case
            "@1": "[&3].&(2,1)"  // [&3]:increments +1 more level compared to the upper one
                                 // &(2,1):replicates the 1st piece represented 
                                 // by asterisk in "*ation" after going 2 levels up
          }
        }
      }
    }
  }
]

http://jolt-demo.appspot.com/网站上的demo是:

相关问题