Home > OS >  how to get a value of a json http response in azure logic app
how to get a value of a json http response in azure logic app

Time:09-27

I'm trying to get "id" value set to a variable by parsing the below output sample. The REST API call will return multiple values as shown below and I'm interested in only getting the "id" value for the particular name that users has provided/set as input in the workflow earlier either by a parameter value or by initializing a variable. How do I do this value extraction in azure logic app?

Any help much appreciated.

[
  {
    "id": 1,
    "name": "xyz-List",
    "data": {
      "urls": [
        "*.test1.com",
        "*.test2.com"
      ],
      "type": "exact"
    },
    "modify_by": "[email protected]",
    "modify_time": "2022-06-29T21:05:27.000Z",
    "modify_type": "Created",
    "pending": 0
  },
  {
    "id": 2,
    "name": "abc-List",
    "data": {
      "urls": [
        "www.mytesting.com"
      ],
      "type": "exact"
    },
    "modify_by": "[email protected]",
    "modify_time": "2022-06-29T21:05:27.000Z",
    "modify_type": "Created",
    "pending": 0
  },
  {
    "id": 3,
    "name": "azure-list",
    "data": {
      "type": "exact",
      "urls": [
        "www.xyz.com",
        "www.azure-test.com"
      ],
      "json_version": 2
    },
    "modify_by": "[email protected]",
    "modify_time": "2022-09-26T01:25:20.000Z",
    "modify_type": "Edited",
    "pending": 0
  }
]

CodePudding user response:

I have reproduced from my end and could able to make it work by parsing your REST API call value. To iterate through the Parsed JSON I have used a for-each loop and extracted the Id using the below expression and set its value to a variable.

@items('For_each')['id']

Below is the complete flow of my logic app

enter image description here

RESULTS:

enter image description here

To reproduce the same in your logic app you can use the below code view that worked for me.

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": [
                    {
                        "data": {
                            "type": "exact",
                            "urls": [
                                "*.test1.com",
                                "*.test2.com"
                            ]
                        },
                        "id": 1,
                        "modify_by": "[email protected]",
                        "modify_time": "2022-06-29T21:05:27.000Z",
                        "modify_type": "Created",
                        "name": "xyz-List",
                        "pending": 0
                    },
                    {
                        "data": {
                            "type": "exact",
                            "urls": [
                                "www.mytesting.com"
                            ]
                        },
                        "id": 2,
                        "modify_by": "[email protected]",
                        "modify_time": "2022-06-29T21:05:27.000Z",
                        "modify_type": "Created",
                        "name": "abc-List",
                        "pending": 0
                    },
                    {
                        "data": {
                            "json_version": 2,
                            "type": "exact",
                            "urls": [
                                "www.xyz.com",
                                "www.azure-test.com"
                            ]
                        },
                        "id": 3,
                        "modify_by": "[email protected]",
                        "modify_time": "2022-09-26T01:25:20.000Z",
                        "modify_type": "Edited",
                        "name": "azure-list",
                        "pending": 0
                    }
                ],
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "For_each": {
                "actions": {
                    "Set_variable": {
                        "inputs": {
                            "name": "Id",
                            "value": "@items('For_each')['id']"
                        },
                        "runAfter": {},
                        "type": "SetVariable"
                    }
                },
                "foreach": "@body('Parse_JSON')",
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Id",
                            "type": "integer"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@outputs('Compose')",
                    "schema": {
                        "items": {
                            "properties": {
                                "data": {
                                    "properties": {
                                        "type": {
                                            "type": "string"
                                        },
                                        "urls": {
                                            "items": {
                                                "type": "string"
                                            },
                                            "type": "array"
                                        }
                                    },
                                    "type": "object"
                                },
                                "id": {
                                    "type": "integer"
                                },
                                "modify_by": {
                                    "type": "string"
                                },
                                "modify_time": {
                                    "type": "string"
                                },
                                "modify_type": {
                                    "type": "string"
                                },
                                "name": {
                                    "type": "string"
                                },
                                "pending": {
                                    "type": "integer"
                                }
                            },
                            "required": [
                                "id",
                                "name",
                                "data",
                                "modify_by",
                                "modify_time",
                                "modify_type",
                                "pending"
                            ],
                            "type": "object"
                        },
                        "type": "array"
                    }
                },
                "runAfter": {
                    "Compose": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

CodePudding user response:

Load this into your tenant. You can use basic expressions with a condition to get your result ...

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_Each_Array_Item": {
                "actions": {
                    "Condition": {
                        "actions": {
                            "Set_ID": {
                                "inputs": {
                                    "name": "ID",
                                    "value": "@item()['id']"
                                },
                                "runAfter": {},
                                "type": "SetVariable"
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "equals": [
                                        "@item()['name']",
                                        "abc-List"
                                    ]
                                }
                            ]
                        },
                        "runAfter": {},
                        "type": "If"
                    }
                },
                "foreach": "@variables('Array Data')",
                "runAfter": {
                    "Initialize_ID": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_Array_Data": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Array Data",
                            "type": "array",
                            "value": [
                                {
                                    "data": {
                                        "type": "exact",
                                        "urls": [
                                            "*.test1.com",
                                            "*.test2.com"
                                        ]
                                    },
                                    "id": 1,
                                    "modify_by": "[email protected]",
                                    "modify_time": "2022-06-29T21:05:27.000Z",
                                    "modify_type": "Created",
                                    "name": "xyz-List",
                                    "pending": 0
                                },
                                {
                                    "data": {
                                        "type": "exact",
                                        "urls": [
                                            "www.mytesting.com"
                                        ]
                                    },
                                    "id": 2,
                                    "modify_by": "[email protected]",
                                    "modify_time": "2022-06-29T21:05:27.000Z",
                                    "modify_type": "Created",
                                    "name": "abc-List",
                                    "pending": 0
                                },
                                {
                                    "data": {
                                        "json_version": 2,
                                        "type": "exact",
                                        "urls": [
                                            "www.xyz.com",
                                            "www.azure-test.com"
                                        ]
                                    },
                                    "id": 3,
                                    "modify_by": "[email protected]",
                                    "modify_time": "2022-09-26T01:25:20.000Z",
                                    "modify_type": "Edited",
                                    "name": "azure-list",
                                    "pending": 0
                                }
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_ID": {
                "inputs": {
                    "variables": [
                        {
                            "name": "ID",
                            "type": "integer"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Array_Data": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "ParameterTest1": {
                "defaultValue": "\"\"",
                "type": "String"
            }
        },
        "triggers": {
            "manual": {
                "inputs": {
                    "method": "GET",
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

My example looks for the name abc-List and if it finds it, it sets the ID variable to be the associated ID of that record it found the name against.

Flow

  • Related