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

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

How to add one object into multiple objects -JOLT

问题

  1. [
  2. {
  3. "country": "canada",
  4. "item": "banana",
  5. "price": 3
  6. },
  7. {
  8. "item": "banana",
  9. "price": 3
  10. }
  11. ]
英文:

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

  1. {
  2. "data": [
  3. {
  4. "item": "banana",
  5. "value": 3
  6. },
  7. {
  8. "item": "banana",
  9. "value": 3
  10. }
  11. ],
  12. "info": {
  13. "place": [
  14. {
  15. "country": "canada"
  16. }
  17. ]
  18. }
  19. }

my code:

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "data": {
  6. "*": {
  7. "item": "[&1].&",
  8. "value": "[&1].price",
  9. "@2,info.place": {
  10. "*": {
  11. "country": "[&1].&"
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }
  18. ]

output:

  1. [
  2. {
  3. "country": [ "canada", "canada" ],
  4. "item": "banana",
  5. "price": 3
  6. },
  7. {
  8. "item": "banana",
  9. "price": 3
  10. }
  11. ]

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" 键旁边,如下所示:

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "data": {
  6. "*": {
  7. "item": "[&1].&",
  8. "value": "[&1].price",
  9. "@2,info.place": {
  10. "*": {
  11. "country": "[&3].&"
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }
  18. ]

这将产生以下结果:

  1. [
  2. {
  3. "country": "canada",
  4. "item": "banana",
  5. "price": 3
  6. },
  7. {
  8. "country": "canada",
  9. "item": "banana",
  10. "price": 3
  11. }
  12. ]
英文:

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

  1. [
  2. {
  3. "operation": "shift",
  4. "spec": {
  5. "data": {
  6. "*": {
  7. "item": "[&1].&",
  8. "value": "[&1].price",
  9. "@2,info.place": {
  10. "*": {
  11. "country": "[&3].&"
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }
  18. ]

which will yield

  1. [
  2. {
  3. "country": "canada",
  4. "item": "banana",
  5. "price": 3
  6. },
  7. {
  8. "country": "canada",
  9. "item": "banana",
  10. "price": 3
  11. }
  12. ]

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:

确定