在大查询中以动态的方式将json分为列
我有以下JSON:
{
"rewards": {
"reward_1": {
"type": "type 1",
"amount": "amount 1"
},
"reward_2": {
"type": "type 2",
"amount": "amount 2"
},
"reward_3": {
"type": "type 3",
"amount": "amount 3"
},
"reward_4": {
"type": "type 4",
"amount": "amount 4"
}
}
}
这个JSON是动态的,我不一定知道它会得到多少奖励,这里是4个,但可以是2或8等。
我想在大查询中编写一个查询,以解析这些值动态地不知道其中有多少,然后将它们分成列,例如:
I have the following JSON:
{
"rewards": {
"reward_1": {
"type": "type 1",
"amount": "amount 1"
},
"reward_2": {
"type": "type 2",
"amount": "amount 2"
},
"reward_3": {
"type": "type 3",
"amount": "amount 3"
},
"reward_4": {
"type": "type 4",
"amount": "amount 4"
}
}
}
This JSON is dynamic and I don't necessarily know how many rewards it will get, here it's 4 but it can be 2 or 8 etc.
I want to write a query in Big Query that will parse those values dynamically without knowing how many of them exist, and then split them into column, like this:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
希望这些有帮助。
max_reward
udf。)奖励
字段中提取每个奖励。pivot
查询使结果成为宽形式。如果需要一个更通用的解决方案,则需要使用BigQuery Dynamic SQL生成枢轴列。我已经在查询中对它们进行了硬编码。
查询:
output:
”单独的列
输出:
Hope these are helpful.
max_reward
UDF.)rewards
field in an iterative way.PIVOT
query.If you want a more generic solution, you need to use BigQuery dynamic SQL to generate PIVOT columns. I've hard-coded them in the query.
query:
output:
▶ Split a
reward
STRUCT column into separate columnsoutput: