为当前比特币市场数据 JSON 转换 属性 名称

Transform property name for JSON of current bitcoin market data

UPDATE - 我修改了原始问题,因此我现在提供可用于轻松设置测试 Azure 逻辑应用程序的代码在 Azure 中重复我的场景。

我有一个 Azure 逻辑应用程序可以从 public API 检索加密货币市场数据并将其转换为 CSV table。

我正在工作流中使用 HTTP 操作执行 API 请求。该操作执行 https://api.wazirx.com/api/v2/trades?market=btcusdt 的 GET。这会产生具有以下结构的响应:

[{"id":239067645,"market":"btcusdt","price":"61999.0","volume":"0.00005","funds":"3.09995","created_at":"2021-10-25T04:00:38Z","side":null},  {"id":239065383,"market":"btcusdt","price":"61966.0","volume":"0.00021","funds":"13.01286","created_at":"2021-10-25T03:57:25Z","side":null}]

我想将其中一个属性的 属性 名称从 API 响应 ("market") 转换为自定义 属性 名称 ("pair"),这样我就可以将以下内容作为我的 Azure 逻辑应用程序 'Create CSV table' 操作的输入:

[{"id":239067645,"pair":"btcusdt","price":"61999.0","volume":"0.00005","funds":"3.09995","created_at":"2021-10-25T04:00:38Z","side":null},{"id":239065383,"pair":"btcusdt","price":"61966.0","volume":"0.00021","funds":"13.01286","created_at":"2021-10-25T03:57:25Z","side":null}]

我目前正在使用以下工作流程和源代码。源代码可直接用于使用 Azure 逻辑应用程序的 'consumption' SKU 在 Azure 中创建逻辑应用程序工作流。工作流在 'Create CSV table step' 处失败,因为它从 'For each'.

的正文中获取空值

我如何调整我的工作流程代码,以便 'Create CSV table' 操作的输入是所需的输入?

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Create_CSV_table": {
                "inputs": {
                    "format": "CSV",
                    "from": "@body('For_each')"
                },
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "type": "Table"
            },
            "For_each": {
                "actions": {
                    "Compose": {
                        "inputs": {
                            "pair": "@items('For_each')?['market']"
                        },
                        "runAfter": {},
                        "type": "Compose"
                    }
                },
                "foreach": "@body('Parse_JSON')",
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "HTTP": {
                "inputs": {
                    "method": "GET",
                    "uri": "https://api.wazirx.com/api/v2/trades?market=btcusdt"
                },
                "runAfter": {},
                "type": "Http"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@body('HTTP')",
                    "schema": {
                        "items": {
                            "properties": {
                                "created_at": {
                                    "type": "string"
                                },
                                "funds": {
                                    "type": "string"
                                },
                                "id": {
                                    "type": "integer"
                                },
                                "market": {
                                    "type": "string"
                                },
                                "price": {
                                    "type": "string"
                                },
                                "side": {},
                                "volume": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "id",
                                "market",
                                "price",
                                "volume",
                                "funds",
                                "created_at",
                                "side"
                            ],
                            "type": "object"
                        },
                        "type": "array"
                    }
                },
                "runAfter": {
                    "HTTP": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    }
}

尝试使用 Logic App 表达式将 JSON 数组对象作为字符串处理,然后将其传输回 JSON 数组。

有关逻辑应用表达式的更多信息:https://docs.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference

请尝试此逻辑应用程序代码:

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": "@json(replace(string(body('HTTP')),'\"market\"','\"pair\"'))",
                "runAfter": {
                    "HTTP": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Create_CSV_table": {
                "inputs": {
                    "format": "CSV",
                    "from": "@outputs('Compose')"
                },
                "runAfter": {
                    "Compose": [
                        "Succeeded"
                    ]
                },
                "type": "Table"
            },
            "HTTP": {
                "inputs": {
                    "method": "GET",
                    "uri": "https://api.wazirx.com/api/v2/trades?market=btcusdt"
                },
                "runAfter": {},
                "type": "Http"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    }
}

我就是这样实现你的要求的

因为Create CSV table 只接受数组值作为输入。我已经初始化了一个数组变量并添加了 Append to array variable 连接器而不是组合然后将其传递给 Create CSV table.

这是逻辑应用程序屏幕截图

这是工作流程

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each": {
                "actions": {
                    "Create_CSV_table": {
                        "inputs": {
                            "format": "CSV",
                            "from": "@variables('Array')"
                        },
                        "runAfter": {},
                        "type": "Table"
                    }
                },
                "foreach": "@variables('Array')",
                "runAfter": {
                    "For_each_2": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "For_each_2": {
                "actions": {
                    "Append_to_array_variable": {
                        "inputs": {
                            "name": "Array",
                            "value": {
                                "created_at": "@items('For_each_2')['created_at']",
                                "funds": "@items('For_each_2')['funds']",
                                "id": "@items('For_each_2')['id']",
                                "pair": "@items('For_each_2')['market']",
                                "price": "@items('For_each_2')['price']",
                                "side": "@items('For_each_2')['side']",
                                "volume": "@items('For_each_2')['volume']"
                            }
                        },
                        "runAfter": {},
                        "type": "AppendToArrayVariable"
                    }
                },
                "foreach": "@body('Parse_JSON')",
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "HTTP": {
                "inputs": {
                    "method": "GET",
                    "uri": "https://api.wazirx.com/api/v2/trades?market=btcusdt"
                },
                "runAfter": {},
                "type": "Http"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Array",
                            "type": "array"
                        }
                    ]
                },
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@body('HTTP')",
                    "schema": {
                        "items": {
                            "properties": {
                                "created_at": {
                                    "type": "string"
                                },
                                "funds": {
                                    "type": "string"
                                },
                                "id": {
                                    "type": "integer"
                                },
                                "market": {
                                    "type": "string"
                                },
                                "price": {
                                    "type": "string"
                                },
                                "side": {},
                                "volume": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "id",
                                "market",
                                "price",
                                "volume",
                                "funds",
                                "created_at",
                                "side"
                            ],
                            "type": "object"
                        },
                        "type": "array"
                    }
                },
                "runAfter": {
                    "HTTP": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {},
                "kind": "Http",
                "type": "Request"
            }
        }
    }
}