将具有字符串或对象值的相同键转换为 Jolt

huangapple go评论72阅读模式
英文:

Jolt - Transform from same key that can have string or an object value

问题

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

I am trying to transform an input data where the same key-value could be string or object. But not able to figure out the correct transformation. Could someone please help. Thanks!

Input

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

Desired output:

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

答案1

得分: 1

你可以使用以下的转换

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&",
        "*ation": {
          "value": "[&2].&(1,1)",
          "*": {
            "@1": "[&3].&(2,1)"
          }
        }
      }
    }
  }
]

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

将具有字符串或对象值的相同键转换为 Jolt

英文:

You can use the following transformation

[
  {
    "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
          }
        }
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is :

将具有字符串或对象值的相同键转换为 Jolt

huangapple
  • 本文由 发表于 2023年6月15日 20:07:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定