首页 > 解决方案 > 使用 Azure 逻辑应用操作更改 JSON 属性名称并删除属性

问题描述

我有一个 Azure 逻辑应用程序,我希望它从公共 API 获取 JSON 并对 JSON 执行一些简单的转换。API 位于https://api.wazirx.com/api/v2/trades?market=btcusdt(用于检索比特币交易数据)。

我目前有以下工作流程操作。我正在使用 Compose 操作步骤来更改原始 API 响应中的 JSON。我想从响应中更改一些属性名称,并从 JSON 中删除其中一个属性。实现这种简单映射的好方法是什么?我目前用于重命名属性的嵌套替换显然很笨拙,我不确定如何删除其中一个属性:

    "actions": {
        "HTTP": {
            "inputs": {
                "headers": {
                    "X-API-Key": "..."
                },
                "method": "GET",
                "queries": {
                },
                "uri": "https://api.wazirx.com/api/v2/trades?market=btcusdt"
            },
            "runAfter": {},
            "type": "Http"
        },
        "Compose": {
            "inputs": "@json(replace(replace(replace(string(body('HTTP')),'\"btcusdt\"','\"BitcoinUsdt\"'), '\"volume\"', '\"BitcoinAmount\"'), '\"funds\"','\"UsdtAmount\"'))",
            "runAfter": {
                "HTTP": [
                    "Succeeded"
                ]
            },
            "type": "Compose"
        },

来自 API 的原始 JSON 示例可在https://api.wazirx.com/api/v2/trades?market=btcusdt获得。以下是数据示例:

[{"id":240144546,"market":"btcusdt","price":"62800.0","volume":"0.00022","funds":"13.816","created_at":"2021-10-26T02:39:07Z","side":null},{"id":240144420,"market":"btcusdt","price":"62800.0","volume":"0.0003","funds":"18.84","created_at":"2021-10-26T02:39:03Z","side":null}]

我想删除“id”及其值,并根据replace我当前的工作流程代码重命名一些属性)。我需要的输出如下:

[{"market":"BitcoinUsdt","price":"62800.0","BitcoinAmount":"0.00022","UsdtAmount":"13.816","created_at":"2021-10-26T02:39:07Z","side":null},{"id":240144420,"market":"btcusdt","price":"62800.0","volume":"0.0003","funds":"18.84","created_at":"2021-10-26T02:39:03Z","side":null}]

实现这种转变的好方法是什么?我一直在尝试使用 For_each 进行 Compose 和 Parse_JSON 操作,但看起来我真的在寻找类似液体模板的东西。这是最简单的方法,还是我可以直接在 Azure 逻辑应用程序中使用类似的方法,而不必将流动模板上传到我的 Azure 集成帐户来进行转换?

标签: azure-logic-apps

解决方案


这是可行的。 在此处输入图像描述

在初始化变量步骤中,我初始化了一个名为 finalresult 的数组变量,其初始值为 []。这将存储 JSON 结果的最终数组。 在此处输入图像描述

在我的 For each step 中,我首先解析每个输出以获取各个属性。然后,我使用属性组合结果并重命名一些属性。以下是我用作输入的内容。

{
  "BitcoinAmount": @{body('Parse_JSON')?['volume']},
  "UsdtAmount": @{body('Parse_JSON')?['funds']},
  "created_at": @{body('Parse_JSON')?['created_at']},
  "market": @{body('Parse_JSON')?['market']},
  "price": @{body('Parse_JSON')?['price']},
  "side": @{body('Parse_JSON')?['side']}
}

最后,我将结果附加到我的 finalresult 数组变量中。 在此处输入图像描述

在 for each 之后,我用我的最终结果响应来响应我的 API 调用。

我的完整逻辑应用程序代码如下。

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each": {
                "actions": {
                    "Append_to_array_variable": {
                        "inputs": {
                            "name": "finalresult",
                            "value": "@outputs('Compose')"
                        },
                        "runAfter": {
                            "Compose": [
                                "Succeeded"
                            ]
                        },
                        "type": "AppendToArrayVariable"
                    },
                    "Compose": {
                        "inputs": {
                            "BitcoinAmount": "@body('Parse_JSON')?['volume']",
                            "UsdtAmount": "@body('Parse_JSON')?['funds']",
                            "created_at": "@body('Parse_JSON')?['created_at']",
                            "market": "@body('Parse_JSON')?['market']",
                            "price": "@body('Parse_JSON')?['price']",
                            "side": "@body('Parse_JSON')?['side']"
                        },
                        "runAfter": {
                            "Parse_JSON": [
                                "Succeeded"
                            ]
                        },
                        "type": "Compose"
                    },
                    "Parse_JSON": {
                        "inputs": {
                            "content": "@items('For_each')",
                            "schema": {
                                "properties": {
                                    "created_at": {
                                        "type": "string"
                                    },
                                    "funds": {
                                        "type": "string"
                                    },
                                    "id": {
                                        "type": "integer"
                                    },
                                    "market": {
                                        "type": "string"
                                    },
                                    "price": {
                                        "type": "string"
                                    },
                                    "side": {},
                                    "volume": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            }
                        },
                        "runAfter": {},
                        "type": "ParseJson"
                    }
                },
                "foreach": "@body('HTTP')",
                "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": "finalresult",
                            "type": "array",
                            "value": []
                        }
                    ]
                },
                "runAfter": {
                    "HTTP": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Response": {
                "inputs": {
                    "body": "@variables('finalresult')",
                    "statusCode": 200
                },
                "kind": "http",
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "type": "Response"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

推荐阅读