Chris Halcrow
Chris Halcrow

Reputation: 31950

Alter JSON property names and remove a property with Azure Logic App Actions

I have an Azure Logic App, and I want it to get JSON from a public API and perform some simple transformations on the JSON. The API is at https://api.wazirx.com/api/v2/trades?market=btcusdt (it's for retrieving Bitcoin trade data).

I currently have the workflow actions below. I'm using a Compose action step, to alter the JSON from the original API response. I want to alter a number of property names from the response, and also remove one of the properties from the JSON. What's a good method to achieve this type of simple mapping? The nested replace I'm currently using for renaming the properties is obviously clumsy, and I'm not sure how to remove one of the properties:

    "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"
        },

A sample of the original JSON from the API is available at https://api.wazirx.com/api/v2/trades?market=btcusdt. Here's a sample of the data:

[{"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}]

I want to remove the "id" and its value, and rename some of the properties as per the replace in my current workflow code). The output I need is as follows:

[{"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}]

What's a good, simple way to achieve this transformation? I've been trying Compose and, Parse_JSON actions with For_each, but it looks like I'm really looking for something like liquid templates. Is this the simplest way, or is there anything similar I can use directly in Azure Logic Apps, instead of having to upload a liquid template to my Azure integration account in order to do a transform?

Upvotes: 2

Views: 1971

Answers (2)

10p
10p

Reputation: 6706

You need to use a single Select action - nothing else:

enter image description here

Result:

enter image description here

Upvotes: 3

Anupam Chand
Anupam Chand

Reputation: 2662

This is something that will work. enter image description here

In Initialize variable step, I have initialized an array variable called finalresult with an initial value of []. This will store the final array of JSON results. enter image description here

In my For each step, I'm first parsing each output to fetch the individual attributes. I'm then composing a result using the attributes and renaming some of the properties. Below is what I'm using as my input.

{
  "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']}
}

Finally I'm appending the result into my finalresult array variable. enter image description here

After the for each, I'm responding to my API call with my finalresult response.

My full logic app code is below.

{
    "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": {}
}

Upvotes: 2

Related Questions