0

I'm trying to group by below JSON Message:

[
    {
        "Id": "1",
        "name": "xxx",
        "age": "20"
    },
    {
        "Id": "1",
        "name": "yyy",
        "age": "52"
    },
    {
        "Id": "5",
        "name": "zzz",
        "age": "59"
    }
]

to

[
    {
        "1": [
            {
                "Id": "1",
                "name": "xxx",
                "age": "20"
            },
            {
                "Id": "1",
                "name": "yyy",
                "age": "52"
            }
        ],
        "5": [
            {
                "Id": "5",
                "name": "zzz",
                "age": "59"
            }
        ]
    }
]

Can someone please help? JSON Array to another JSON array with group by on fly. NOTE: I don't want to use the Azure function for this.

Thanks, Deepak

Skin
  • 9,085
  • 2
  • 13
  • 29
Deepak Shaw
  • 461
  • 3
  • 6
  • Can I ask why not a function? – Skin Aug 08 '23 at 06:32
  • Just don't want to complicate this, if possible to achieve in LA, that would be the preferable solution. Maintenance would be in control. – Deepak Shaw Aug 08 '23 at 06:38
  • Soooooo you want to avoid code completely because those maintaining it aren't going to be code literate? What if you could do it in LA with code but no need for a function? – Skin Aug 08 '23 at 07:09
  • Are you open to a third party connector? The one I am proposing you use is quite affordable AND it comes with a whole raft of very useful operations. Depending on how many calls you need per month, it is as cheap as $2 USD a month. In all honesty, to do what you’re asking without the use of a more advanced approach is going to be a pain. – Skin Aug 12 '23 at 21:15

2 Answers2

0

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

Design:

enter image description here

JavaScript:

const rithinput= [
    {
        "Id": "1",
        "name": "xx",
        "age": "20"
    },
    {
        "Id": "1",
        "name": "yyy",
        "age": "52"
    },
    {
        "Id": "5",
        "name": "zz",
        "age": "59"
    }
];

const group = {};

rithinput.forEach(item => {
    const id = item.Id;
    if (!group[id]) {
        group[id] = [];
    }
    group[id].push(item);
});

const result = [group];

return result;

Output: enter image description here

enter image description here

To execute inline script you need have integration account integrated with logic app.

RithwikBojja
  • 5,069
  • 2
  • 3
  • 7
  • Are you using a logic app consumption plan? or something else? I'm getting below error while using execute JS code connector - The workflow must be associated with an integration account to use the workflow run action 'Execute_JavaScript_Code' of type 'JavaScriptCode'. – Deepak Shaw Aug 10 '23 at 06:21
  • I have mentioned in the answer clearly that you need to have an integrated account and I am using consumption plan. To [integrate account](https://learn.microsoft.com/en-us/azure/logic-apps/logic-apps-enterprise-integration-create-integration-account?tabs=azure-portal%2Cconsumption) [Image](https://i.imgur.com/ib1MeO7.png) – RithwikBojja Aug 10 '23 at 06:23
  • Thanks, but is there any alternative way to achieve the same, using without an integration account ? – Deepak Shaw Aug 11 '23 at 07:03
  • No, there is not, you need to use 3rd party connectors which are not free!, any ways glad it helped. – RithwikBojja Aug 11 '23 at 10:21
0

This is an example of how to do it using nothing but standard actions in LogicApps ...

enter image description here

Create a new LogicApp and load the definition into it via the Code View.

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": [
                    "@variables('Result Object')"
                ],
                "runAfter": {
                    "For_Each_Item": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "For_Each_Item": {
                "actions": {
                    "Does_Current_ID_Exist_In_Result_Object": {
                        "actions": {
                            "Add_Property_(Step_1)": {
                                "inputs": "@setProperty(variables('Result Object'), variables('Current ID'), body('Filter_Data_For_Current_ID'))",
                                "runAfter": {
                                    "Filter_Data_For_Current_ID": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "Compose"
                            },
                            "Add_Property_(Step_2)": {
                                "inputs": {
                                    "name": "Result Object",
                                    "value": "@outputs('Add_Property_(Step_1)')"
                                },
                                "runAfter": {
                                    "Add_Property_(Step_1)": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "SetVariable"
                            },
                            "Filter_Data_For_Current_ID": {
                                "inputs": {
                                    "from": "@variables('Data')",
                                    "where": "@equals(item()['Id'], variables('Current ID'))"
                                },
                                "runAfter": {},
                                "type": "Query"
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "equals": [
                                        "@variables('Result Object')?[variables('Current ID')]",
                                        "@null"
                                    ]
                                }
                            ]
                        },
                        "runAfter": {
                            "Set_Current_ID": [
                                "Succeeded"
                            ]
                        },
                        "type": "If"
                    },
                    "Set_Current_ID": {
                        "inputs": {
                            "name": "Current ID",
                            "value": "@{item()['Id']}"
                        },
                        "runAfter": {},
                        "type": "SetVariable"
                    }
                },
                "foreach": "@variables('Data')",
                "runAfter": {
                    "Initialize_Current_ID": [
                        "Succeeded"
                    ]
                },
                "runtimeConfiguration": {
                    "concurrency": {
                        "repetitions": 1
                    }
                },
                "type": "Foreach"
            },
            "Initialize_Current_ID": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Current ID",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Result_Object": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Data": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Data",
                            "type": "array",
                            "value": [
                                {
                                    "Id": "1",
                                    "age": "20",
                                    "name": "xxx"
                                },
                                {
                                    "Id": "1",
                                    "age": "52",
                                    "name": "yyy"
                                },
                                {
                                    "Id": "5",
                                    "age": "59",
                                    "name": "zzz"
                                }
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_Result_Object": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Result Object",
                            "type": "object",
                            "value": {}
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Data": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {},
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}
Skin
  • 9,085
  • 2
  • 13
  • 29