skip to Main Content

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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": {}
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search