gpt4 book ai didi

azure - 将 JSON 数据行转换为列

转载 作者:行者123 更新时间:2023-12-02 23:44:46 24 4
gpt4 key购买 nike

我需要以下方面的帮助。我有这样的数据

[{
"id": "0001",
"type": "donut",
"name": "Cake",
"topping":
[
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}]

我想将其转换为以下内容

enter image description here

参数将是动态的或多个,而不仅仅是 Chocolate 和 Maple )

我想创建一个流分析查询来处理这些数据并将其存储到目标表中,目标表中已经有这些列,如 Id、Name、Type、Chocolate、Maple......请帮助我。

最佳答案

您可以通过udf获得帮助在 ASA 中。

UDF 代码:

function main(arg) {
var array = arg.topping;
var map = {};
map["id"] = arg.id;
map["type"] = arg.type;
map["name"] = arg.name;
for(var i=0;i<array.length;i++){
var key=array[i].type;
map[key] = array[i].id;
}
return map;
}

SQL:

WITH 
c AS
(
SELECT
udf.processArray(jsoninput) as result
from jsoninput
)

select c.result
INTO
jaycosmos
from c

示例数据:

[{
"id": "0001",
"type": "donut",
"name": "Cake",
"topping":
[
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut2",
"name": "Cake2",
"topping":
[
{ "id": "5005", "type": "Chocolate" }
]
}
]

输出:

enter image description here

关于azure - 将 JSON 数据行转换为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53075735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com