0

I have an array of objects and want to retrieve the value by key.

{
  "foo.companies": {
    "type": "Array",
    "value": [
      {
        "foo": [
          "123"
        ]
      },
      {
        "bar": [
          "123"
        ]
      }
    ]
  }
}

I used the following code but it failed.

item()?.item()['dynamic_key_field']?[0]

Here I've a dynamic field and I want to retrieve by the dynamic key field the value.

PHP example

$bar = ['foo' => 123];
$dynamicField = 'foo';
$result = $bar[$dynamicField];
// $result contains 123
Skin
  • 9,085
  • 2
  • 13
  • 29
peterhillie
  • 107
  • 1
  • 13
  • Do you not know the property names at run time? Meaning, does it change payload on payload and are completely dynamic? – Skin May 30 '23 at 21:01

2 Answers2

1

Based on your question, I'm going to make the assumption that you want to process and extract values dynamically based on the property in the JSON.

Given that be the case, you can't just use standard operations for that, you'll need something a bit smarter. You can use Azure Functions but I haven't gone with that as an answer because I think you can do it with a 3rd party connector that will 1) cut down the time and 2) complete your requirement entirely within your flow.

Using two operations from the Advanced Data Operations connector, you can achieve your desired result.

Note: The answer is (relatively) specific to the JSON you provided. It may need tweaking depending on the actual JSON you are working with.

This flow is an example of how to isolate the data so that you can filter on it ...

Flow

Step 1.

This is nothing more than your JSON in an object variable so it can be used in subsequent operations.

Step 2.

C# Script Execute

Using the C# Script Execute operation, you can paste in this code ...

JObject jObject = JObject.FromObject(parameters.json);
IEnumerable<JArray> jArrayItems = jObject.Descendants().OfType<JArray>().Where(x => x.Count == 1);

StringBuilder sb = new StringBuilder();
var sw = new System.IO.StringWriter(sb);

using (JsonWriter jw = new JsonTextWriter(sw))
{
    jw.WriteStartObject();

    foreach (var item in jArrayItems)
    {
        jw.WritePropertyName(((JProperty)item.Parent).Name);
        jw.WriteValue(item.First.ToString());
    }

    jw.WriteEndObject();
}

return sw.ToString();

... with the parameters like thus ...

{
  "json": @{variables('JSON')}
}

... and that will give you a JSON string that have the properties, with values, that you're wanting to get your hands on.

Result 1

That only gets your half way there though.

By using the Json Properties to Name Value Pair Array operation, you can then turn that JSON into something that can be easily queried ...

Step 2

Expression = json(body('C#_Script_Execute')['returnValue'])

It will return this for you and it's from here that you can perform a filter to get the specific values based on the name of the relevant property by simply using the Filter array operation from the standard actions ...

[
  {
    "propertyName": "foo",
    "propertyType": "String",
    "propertyValue": "123"
  },
  {
    "propertyName": "bar",
    "propertyType": "String",
    "propertyValue": "123"
  }
]
Skin
  • 9,085
  • 2
  • 13
  • 29
0

I have reproduced in my environment and got expected results as below:

Input:

{
    "foo.companies": {
        "type": "Array",
        "value": [{
                "foo": ["123"]
            },
            {
                "bar": ["135"]
            }
        ]
    }
}

enter image description here

Now In Design: enter image description here

I have given hardcoded value like bar and foo(you have given you will get it dynamically and you can save dynamic value in a variable)

Then Parse Json:

enter image description here

Shema :

{
    "properties": {
        "foo.companies": {
            "properties": {
                "type": {
                    "type": "string"
                },
                "value": {
                    "items": {
                        "properties": {
                            "bar": {
                                "items": {
                                    "type": "string"
                                },
                                "type": "array"
                            },
                            "foo": {
                                "items": {
                                    "type": "string"
                                },
                                "type": "array"
                            }
                        },
                        "required": [],
                        "type": "object"
                    },
                    "type": "array"
                }
            },
            "type": "object"
        }
    },
    "type": "object"
}

Then got the values into a array variable and then initialized a empty variable to get the value at end:

enter image description here

Then in for each loop: enter image description here

Then compose for seeing output purpose:

enter image description here Output:

foo variable value is retrieved:

enter image description here

bar value is retrieved:

enter image description here

Code view:(For easy replicating the design)

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": "@variables('var3')",
                "runAfter": {
                    "For_each": [
                        "Succeeded",
                        "Failed"
                    ]
                },
                "type": "Compose"
            },
            "For_each": {
                "actions": {
                    "Condition": {
                        "actions": {
                            "Append_to_string_variable": {
                                "inputs": {
                                    "name": "var3",
                                    "value": "@item()[variables('var1')][0]"
                                },
                                "runAfter": {},
                                "type": "AppendToStringVariable"
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "equals": [
                                        "@items('For_each')",
                                        "@items('For_each')"
                                    ]
                                }
                            ]
                        },
                        "runAfter": {},
                        "type": "If"
                    }
                },
                "foreach": "@variables('var2')",
                "runAfter": {
                    "Initialize_variable_3": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "var2",
                            "type": "array",
                            "value": "@body('Parse_JSON')?['foo.companies']?['value']"
                        }
                    ]
                },
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_variable_2": {
                "inputs": {
                    "variables": [
                        {
                            "name": "var1",
                            "type": "string",
                            "value": "foo"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_variable_3": {
                "inputs": {
                    "variables": [
                        {
                            "name": "var3",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@triggerBody()",
                    "schema": {
                        "properties": {
                            "foo.companies": {
                                "properties": {
                                    "type": {
                                        "type": "string"
                                    },
                                    "value": {
                                        "items": {
                                            "properties": {
                                                "bar": {
                                                    "items": {
                                                        "type": "string"
                                                    },
                                                    "type": "array"
                                                },
                                                "foo": {
                                                    "items": {
                                                        "type": "string"
                                                    },
                                                    "type": "array"
                                                }
                                            },
                                            "required": [],
                                            "type": "object"
                                        },
                                        "type": "array"
                                    }
                                },
                                "type": "object"
                            }
                        },
                        "type": "object"
                    }
                },
                "runAfter": {
                    "Initialize_variable_2": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}
RithwikBojja
  • 5,069
  • 2
  • 3
  • 7