如何使用 JOLT 将包含在列表中的 JSON 项目拆分为多个项目?

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

How to unlist JSON item with lists into multiple items using JOLT?

问题

以下是翻译好的部分:

[
  {
    "cume": 150,
    "region": "North",
    "type": "total"
  },
  {
    "cume": 200,
    "region": "North",
    "type": "app"
  },
  {
    "cume": 300,
    "region": "North",
    "type": "web"
  },
  {
    "cume": 360,
    "region": "North",
    "type": "registered"
  },
  {
    "cume": 1000,
    "region": "South",
    "type": "total"
  },
  {
    "cume": 1200,
    "region": "South",
    "type": "app"
  },
  {
    "cume": 3500,
    "region": "South",
    "type": "web"
  },
  {
    "cume": 6200,
    "region": "South",
    "type": "registered"
  }
]

不包括代码部分的翻译已提供。

英文:

I have the input raw JSON file like this below:

[
  {
    "itemId": "001",
    "region": "North",
    "cume": [
      150,
      200,
      300,
      360
    ],
    "type": [
      "total",
      "app",
      "web",
      "registered"
    ]
  },
  {
    "itemId": "002",
    "region": "South",
    "cume": [
      1000,
      1200,
      3500,
      6200
    ],
    "type": [
      "total",
      "app",
      "web",
      "registered"
    ]
  }
]

And my expected output is:

[
  {
    "cume": 150,
    "region": "North",
    "type": "total"
  },
  {
    "cume": 200,
    "region": "North",
    "type": "app"
  },
  {
    "cume": 300,
    "region": "North",
    "type": "web"
  },
  {
    "cume": 360,
    "region": "North",
    "type": "registered"
  },
  {
    "cume": 1000,
    "region": "South",
    "type": "total"
  },
  {
    "cume": 1200,
    "region": "South",
    "type": "app"
  },
  {
    "cume": 3500,
    "region": "South",
    "type": "web"
  },
  {
    "cume": 6200,
    "region": "South",
    "type": "registered"
  }
]

What I have tried is below (it's not working)

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*s": {
          "*": {
            "@": "[&1].&(2,1)", 
            "@(2,region)": "[&1].region"
          }
        }
      }
    }
  }
]

My current output converts everything into a single list. For example, for the region field, the output has four items inside instead of a single item.

答案1

得分: 1

这是您的 jolt 规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "cume": {
          "*": {
            "@": "[&3][&1].&2",
            "@(2,region)": "[&3][&1].region",
            "@(2,type[&])": "[&3][&1].type"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]
英文:

This is your jolt spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "cume": {
          "*": {
            "@": "[&3][&1].&2",
            "@(2,region)": "[&3][&1].region",
            "@(2,type[&])": "[&3][&1].type"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

huangapple
  • 本文由 发表于 2023年5月30日 06:33:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360651.html
匿名

发表评论

匿名网友

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

确定