gpt4 book ai didi

azure - 如何在应用程序逻辑中转换 JSON?

转载 作者:行者123 更新时间:2023-12-03 00:19:22 25 4
gpt4 key购买 nike

我从日志分析查询 API HTTP 操作调用中得到了 json 结果:

{
"tables": [
{
"name": "PrimaryResult",
"columns": [
{
"name": "TimeGenerated",
"type": "datetime"
},
{
"name": "_queue",
"type": "string"
},
{
"name": "_messages",
"type": "real"
}
],
"rows": [
[
"2022-06-03T03:20:00Z",
"queue1",
8073
],
[
"2022-06-03T03:20:00Z",
"queue2",
570
]
]
}
]
}

我需要将其转换为以下格式,这本质上是一个 adaptivecards.io

{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"text": "**TimeGenerated**" //Columns[0].Name
},
{
"type": "TextBlock",
"text": "2022-06-03T03:20:00Z" //Rows[0][0]
},
{
"type": "TextBlock",
"text": "2022-06-03T03:20:00Z" //Rows[1][0]
}
]
},
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"text": "**_queue**" //Columns[1].Name
},
{
"type": "TextBlock",
"text": "queue1" //Rows[0][1]
},
{
"type": "TextBlock",
"text": "queue2" //Rows[1][1]
}
]
},
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"text": "**_messages**"
},
{
"type": "TextBlock",
"text": "8073"
},
{
"type": "TextBlock",
"text": "570"
}
]
}
]
}

我该怎么做?我尝试使用嵌套的 for 循环操作,但是我一直在编写可以插入到正确位置的最终变量。

最好提前保存一个转换后的变量,这样我就可以在后期将其作为 POST 到另一个 webhook 中包含。

编辑:虽然列是静态的,但数字行是动态的。

最佳答案

创建一个 LogicApp 并加载此定义以了解如何操作...

{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Initialize_AdaptiveCard_JSON": {
"inputs": {
"variables": [
{
"name": "XML",
"type": "string",
"value": "{\n \"type\": \"ColumnSet\",\n \"columns\": [\n {\n \"type\": \"Column\",\n \"items\": [\n {\n \"type\": \"TextBlock\",\n \"text\": \"**@{variables('Object Variable')['tables'][0]['columns'][0]['Name']}**\"\n },\n {\n \"type\": \"TextBlock\",\n \"text\": \"@{variables('Object Variable')['tables'][0]['rows'][0][0]}\"\n },\n {\n \"type\": \"TextBlock\",\n \"text\": \"@{variables('Object Variable')['tables'][0]['rows'][1][0]}\"\n }\n ]\n },\n {\n \"type\": \"Column\",\n \"items\": [\n {\n \"type\": \"TextBlock\",\n \"text\": \"**@{variables('Object Variable')['tables'][0]['columns'][1]['Name']}**\"\n },\n {\n \"type\": \"TextBlock\",\n \"text\": \"@{variables('Object Variable')['tables'][0]['rows'][0][1]}\"\n },\n {\n \"type\": \"TextBlock\",\n \"text\": \"@{variables('Object Variable')['tables'][0]['rows'][1][1]}\"\n }\n ]\n },\n {\n \"type\": \"Column\",\n \"items\": [\n {\n \"type\": \"TextBlock\",\n \"text\": \"**@{variables('Object Variable')['tables'][0]['columns'][2]['Name']}**\"\n },\n {\n \"type\": \"TextBlock\",\n \"text\": \"@{variables('Object Variable')['tables'][0]['rows'][0][2]}\"\n },\n {\n \"type\": \"TextBlock\",\n \"text\": \"@{variables('Object Variable')['tables'][0]['rows'][1][2]}\"\n }\n ]\n }\n ]\n}"
}
]
},
"runAfter": {
"Initialize_Object": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Initialize_Object": {
"inputs": {
"variables": [
{
"name": "Object Variable",
"type": "object",
"value": {
"tables": [
{
"columns": [
{
"name": "TimeGenerated",
"type": "datetime"
},
{
"name": "_queue",
"type": "string"
},
{
"name": "_messages",
"type": "real"
}
],
"name": "PrimaryResult",
"rows": [
[
"2022-06-03T03:20:00Z",
"queue1",
8073
],
[
"2022-06-03T03:20:00Z",
"queue2",
570
]
]
}
]
}
}
]
},
"runAfter": {},
"type": "InitializeVariable"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"Recurrence": {
"evaluatedRecurrence": {
"frequency": "Month",
"interval": 12
},
"recurrence": {
"frequency": "Month",
"interval": 12
},
"type": "Recurrence"
}
}
},
"parameters": {}
}

基本上,您只需导航到 JSON 的特定部分即可检索每个单独值所需的内容。

这是我正在谈论的一个例子......

variables('Object Variable')['tables'][0]['columns'][1]['Name']

...这将使您到达第一个表中第二列的 name 属性。

关于azure - 如何在应用程序逻辑中转换 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72486237/

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