2

Using Azure Logic Apps, i'm trying to transform JSON array to JSON object. For example, if i have an array as:

[
  {
    name: 'john'
    id: '1'
  },
  {
    name: 'sarah'
    id: '2'
  },
]

I'm would like to have as output:

{
'1': 'john',
'2': 'sarah'
}
1
  • Try using "Parse JSON" connector to parse your JSON array - [ { name: 'john' id: '1' }, { name: 'sarah' id: '2' }, ]. Initialize empty ([]) and "Array" type variable named "x". Then use "For Each" connector on the generated "Parse JSON output". Inside foreach, use "Append to array variable" on "x" to add your custom output - {'1': 'john', '2': 'sarah'}. Let me know, if this works. Commented Aug 22, 2020 at 21:28

3 Answers 3

5

First init Json Result:

enter image description here

Then in foreach loop (TestArray is our array with data) add compose with Expression "addProperty(variables('JsonResult'),item()?['id'],item()?['name'])" and set variable with output from compose:

enter image description here

In the foreach settings be sure to enable concurrency control, and set the Degree of Parallelism to 1. This is to ensure a sequential run and the variable not being written over in between.

Example run: enter image description here enter image description here

Code for LA:

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each": {
                "actions": {
                    "Add_property": {
                        "inputs": "@addProperty(variables('JsonResult'),item()?['id'],item()?['name'])",
                        "runAfter": {},
                        "type": "Compose"
                    },
                    "Set_variable": {
                        "inputs": {
                            "name": "JsonResult",
                            "value": "@outputs('Add_property')"
                        },
                        "runAfter": {
                            "Add_property": [
                                "Succeeded"
                            ]
                        },
                        "type": "SetVariable"
                    }
                },
                "foreach": "@variables('TestArray')",
                "runAfter": {
                    "Init_Json_Result": [
                        "Succeeded"
                    ]
                },
                "runtimeConfiguration": {
                    "concurrency": {
                        "repetitions": 1
                    }
                },
                "type": "Foreach"
            },
            "Init_Array": {
                "inputs": {
                    "variables": [
                        {
                            "name": "TestArray",
                            "type": "array",
                            "value": [
                                {
                                    "id": "1",
                                    "name": "john"
                                },
                                {
                                    "id": "2",
                                    "name": "sarah"
                                }
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Init_Json_Result": {
                "inputs": {
                    "variables": [
                        {
                            "name": "JsonResult",
                            "type": "object",
                            "value": {}
                        }
                    ]
                },
                "runAfter": {
                    "Init_Array": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Test_result": {
                "inputs": "@variables('JsonResult')",
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great that's what i need
4

As mentioned in the comments above, posting this as an answer. You can follow below steps to achieve this:

  1. Just for testing the flow, add Initialize variable action: enter image description here

  2. Parse the input Array: enter image description here

  3. Then, Initialize empty array variable: enter image description here

  4. Then, use For Each control and inside it you can use Append to array variable action: enter image description here

Now, you are good to use output variable, which has your resultant array.

Tested result: enter image description here

JSON code for this Logic App:

{
    "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": "output",
                            "value": {
                                "@{items('For_each')['id']}": "@{items('For_each')['name']}"
                            }
                        },
                        "runAfter": {},
                        "type": "AppendToArrayVariable"
                    }
                },
                "foreach": "@body('Parse_JSON')",
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "HTTP": {
                "inputs": {
                    "body": "@variables('output')",
                    "method": "POST",
                    "uri": "https://prod-33.westeurope.logic.azure.com:443/workflows/a656267dd18d46d2aa21e79e4012ba29/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=eBgZCaKgCDWiMWjm7JGNNb1-QyXbnT5AlVgBQt1GF48"
                },
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "type": "Http"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "output",
                            "type": "array",
                            "value": []
                        }
                    ]
                },
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_variable_2": {
                "inputs": {
                    "variables": [
                        {
                            "name": "input",
                            "type": "array",
                            "value": [
                                {
                                    "id": "1",
                                    "name": "john"
                                },
                                {
                                    "id": "2",
                                    "name": "sarah"
                                }
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@variables('input')",
                    "schema": {
                        "items": {
                            "properties": {
                                "id": {
                                    "type": "string"
                                },
                                "name": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "name",
                                "id"
                            ],
                            "type": "object"
                        },
                        "type": "array"
                    }
                },
                "runAfter": {
                    "Initialize_variable_2": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

3 Comments

the output would an object not an array though
Output is an array variable and it will be have an array of these custom objects.
I have updated snippets also which show output variable value.
0

In my efforts to get something similar I ended up using a combination of Select, Compose, and the replace function to get the desired result.

I set up an app with the following workflow:

Example Workflow

Using a Parse JSON with the following content as an example:

[
  {
    "name": "john",
    "id": 1
  },
  {
    "name": "sarah",
    "id": 2
  }
]

Step 1 uses a Select with value from the previous node, and a map of @{item()['id']}:@{item()['name']}

Workflow Step 1

Step 2 is a Compose that converts the array from Step 1 into a string, and replaces square brackets and other characters to create a valid Object, and then converting it back into JSON json(replace(replace(replace(string(body('Key-Value_Map_Step_1')), '[', '{'), ']', '}'), ':', '\":\"'))

Workflow Step 2

Which eventually resulted in the desired object format

{
  "1": "john",
  "2": "sarah"
}

This only really works for relatively simple arrays, as using the replace function could easily and accidently modify parts of the data that you don't want it to. In my case though, it worked pretty well as a relatively fast way to convert an array to a key-value object without using For Each loops or Inline-Code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.