azure-logic-apps - 使用 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 集成帐户来进行转换?
解决方案
在初始化变量步骤中,我初始化了一个名为 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": {}
}
推荐阅读
- reactjs - 我的元素没有相应地移动到 onPanResponderMove
- c# - WebApi 自定义 BufferedMediaTypeFormatter WriteToStream 永远不会被调用
- sql - sql(4 不同列) 中的 Dense_rank 查询
- sql - 使用 SQL 中多列的值创建列
- react-native - 如何手动链接库?
- java - 默认情况下总是在菜单切换中运行
- javascript - 为什么等待/异步的结果未定义?
- azure - 将 Azure 自定义脚本扩展从 Blob 添加到 Powershell 的虚拟机规模集
- arrays - 使用多重索引枚举多线性映射的 Swift 算法:[Int]
- api - 如何自动检测 sip/ip 电话的内部号码