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

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

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

问题

  1. [
  2. {
  3. "id": "id1",
  4. "loc": "loc-123"
  5. },
  6. {
  7. "id": "id2",
  8. "loc": "loc-789"
  9. },
  10. {
  11. "id": "id3",
  12. "loc": "loc-666"
  13. }
  14. ]
英文:

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

  1. [
  2. {
  3. "id": "id1",
  4. "location": {
  5. "value": "loc-123"
  6. }
  7. },
  8. {
  9. "id": "id2",
  10. "location": {
  11. "value": "loc-789"
  12. }
  13. },
  14. {
  15. "id": "id3",
  16. "location": "loc-666"
  17. }
  18. ]

Desired output:

  1. [
  2. {
  3. "id": "id1",
  4. "loc": "loc-123"
  5. },
  6. {
  7. "id": "id2",
  8. "loc": "loc-789"
  9. },
  10. {
  11. "id": "id3",
  12. "loc": "loc-666"
  13. }
  14. ]

答案1

得分: 1

你可以使用以下的转换

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "*": {
  6. "*": "[&1].&",
  7. "*ation": {
  8. "value": "[&2].&(1,1)",
  9. "*": {
  10. "@1": "[&3].&(2,1)"
  11. }
  12. }
  13. }
  14. }
  15. }
  16. ]

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

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

英文:

You can use the following transformation

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "*": {
  6. "*": "[&1].&", // generates array-wise([ ]) results after going one (&1) level up
  7. // the tree to reach the level of indexes within the array
  8. "*ation": {
  9. "value": "[&2].&(1,1)",// increments +1 more level compared to the upper one
  10. "*": { // else case
  11. "@1": "[&3].&(2,1)" // [&3]:increments +1 more level compared to the upper one
  12. // &(2,1):replicates the 1st piece represented
  13. // by asterisk in "*ation" after going 2 levels up
  14. }
  15. }
  16. }
  17. }
  18. }
  19. ]

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:

确定