Raj
Raj

Reputation: 95

Logicapp Expression to read Dynamic Json path - read child element where parent path may change but hierarchy remaining same

Hope all well.

I am in need of creating logicapp expression for reading child element in json where name of element & hierarchy remains same but parent name can be changing.

for example : JSON-1 :

{
  "root": {
    "abc1": {
      "abc2": [
        {
          "element": "value1",
          "element2": "value"
        },
        {
          "element": "value2",
          "element2": "valu2"
        }
      ]
    }
  }
}
                 

JSON-2 :

{
  "root": {
    "xyz1": {
      "xyz2": [
        {
          "element": "value1",
          "element2": "value"
        },
        {
          "element": "value2",
          "element2": "valu2"
        }
      ]
    }
  }
}

I have tried these but no luck approach-1: @{body('previous-action')?['']?['']?['element'] approach-2: @{body('previous-action')???['element']

Please let me know if anyone encountered this situation. Many thanks in advance.

Upvotes: 0

Views: 654

Answers (1)

Skin
Skin

Reputation: 11197

I tend to find that converting the JSON to xml (at least in your case) is the simplest solution. Then when you've done that, you can't use XPath to simply make your selection.

Flow

Flow

In basic terms ...

  1. I've defined a variable of type object that contains your JSON.
  2. I then convert that JSON object to XML using this expression xml(variables('JSON Object'))
  3. Next, initialize a variable is called Elements of type array (given you have multiple of them). The expression for setting that variable is where the smarts come in. That expression is ... xpath(xml(variables('XML')), '//element/text()') and it's getting the inner text of all element nodes in the XML.
  4. Finally, loop through the results.

If you needed to take it up a level and get the second element then you'd need to change your xpath query to be a lot more generic so you can get the element2 nodes (and 3, 4, 5, etc. if they existed) in each array as well.

Note: I've stuck to your specific question of looking for element.

Result

Resulting Array

Result

This definition (which can be loaded directly into your tenant) demonstrates the thinking ...

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_Each_Element": {
                "actions": {
                    "Set_Element": {
                        "inputs": {
                            "name": "Element",
                            "value": "@{item()}"
                        },
                        "runAfter": {},
                        "type": "SetVariable"
                    }
                },
                "foreach": "@variables('Elements')",
                "runAfter": {
                    "Initialize_Element": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_Element": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Element",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Elements": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Elements": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Elements",
                            "type": "array",
                            "value": "@xpath(xml(variables('XML')), '//element/text()')"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_XML": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_JSON_Object": {
                "inputs": {
                    "variables": [
                        {
                            "name": "JSON Object",
                            "type": "object",
                            "value": {
                                "root": {
                                    "abc1": {
                                        "abc2": [
                                            {
                                                "element": "value1",
                                                "element2": "value"
                                            },
                                            {
                                                "element": "value2",
                                                "element2": "valu2"
                                            }
                                        ]
                                    }
                                }
                            }
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_XML": {
                "inputs": {
                    "variables": [
                        {
                            "name": "XML",
                            "type": "string",
                            "value": "@{xml(variables('JSON Object'))}"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_JSON_Object": [
                        "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": {}
}

Upvotes: 1

Related Questions