如何将一个对象添加到多个对象中 – JOLT

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

How to add one object into multiple objects -JOLT

问题

[
  {
    "country": "canada",
    "item": "banana",
    "price": 3
  },
  {
    "item": "banana",
    "price": 3
  }
]
英文:

i want to merge the info key data into the rest objects

 {
  "data": [
    {
      "item": "banana",
      "value": 3
    },
    {
      "item": "banana",
      "value": 3
    }
  ],
  "info": {
    "place": [
      {
        "country": "canada"
      }
    ]
  }
}

my code:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "item": "[&1].&",
          "value": "[&1].price",
          "@2,info.place": {
            "*": {
              "country": "[&1].&"
            }
          }
        }
      }
    }
  }
]

output:

[
  {
    "country": [ "canada", "canada" ],
    "item": "banana",
    "price": 3
  },
  {
    "item": "banana",
    "price": 3
  }
]

so country should not be an array, of course, i can use cardinality to remove the array but country is not added to the last object.

答案1

得分: 2

你应该跟踪data数组的索引,而不是place数组的索引,以便遍历索引 01,而不仅仅停留在索引 0

因此,你应该将 [&1] 转换为 [&3],放在 "country" 键旁边,如下所示:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "item": "[&1].&",
          "value": "[&1].price",
          "@2,info.place": {
            "*": {
              "country": "[&3].&"
            }
          }
        }
      }
    }
  }
]

这将产生以下结果:

[
  {
    "country": "canada",
    "item": "banana",
    "price": 3
  },
  {
    "country": "canada",
    "item": "banana",
    "price": 3
  }
]
英文:

You should track the index of data array, not of place array so that to walk the indexes 0 and 1, to not stay only on the index 0.

So, you should convert [&1] to [&3] next to "country" key such as

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "item": "[&1].&",
          "value": "[&1].price",
          "@2,info.place": {
            "*": {
              "country": "[&3].&"
            }
          }
        }
      }
    }
  }
]

which will yield

[
  {
    "country": "canada",
    "item": "banana",
    "price": 3
  },
  {
    "country": "canada",
    "item": "banana",
    "price": 3
  }
]

huangapple
  • 本文由 发表于 2023年6月19日 19:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506334.html
匿名

发表评论

匿名网友

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

确定