Skip to Content
0
Jan 12, 2023 at 02:09 PM

SAP CPI Enrich using Groovy

130 Views Last edit Jan 12, 2023 at 02:16 PM 4 rev

how do I enrich information from one JSON into another JSON using groovy based on a key value ? As shown below based on the value of the key "Id" in MainJson, I need to look up "id" in SecondJson. When a match is found, I need to get the object "name" from SecondJson and append it to MainJson

MainJson

[
    {
        "webshop": [
            {
                "id": "1168190", 
                "type": "segment",
                "values": [
                    {
                        "id": "1168191",
                        "type": "application",
                        "values": [
                            {
                                "id": "1168192",
                                "type": "productRange"
                            },
                            {
                                "id": "1168192",
                                "type": "productRange"
                            }
                        ]
                    },
                    {
                        "id": "1168194",
                        "type": "application",
                        "values": [
                            {
                                "id": "1168195",
                                "type": "productRange"
                            },
                            {
                                "id": "1168196",
                               "type": "productRange"
                            }
                        ]
                    }                   
                ]
            }
        ]
    }
]<br>

SecondJson

{
    "ProductCategorization": [
        {
            "id": "1168190",
            "name": [
                {
                    "en": "Irrigation"
                },
                {
                    "es": "Riego"
                }
            ]
        },
        {
            "id": "1168191",
            "name": [
                {
                    "ES": "Kenadrain"
                },
                {
                    "EN": "Kenadrain"
                }
            ]
        },
        {
            "id": "1168192",
            "name": [
                {
                    "ES": "Fluxol"
                },
                {
                    "EN": "Fluxol"
                }
            ]
        },
        {
            "id": "1168193",
           "name": [
                {
                    "EN": "PP System Jimten"
                },
                {
                    "ES": "PP System Jimten"
                }
            ]
        },
        {
            "id": "1168194",
          "name": [
                {
                    "EN": "Durapipe Superflo ABS Metric"
                },
                {
                    "ES": "Durapipe Superflo ABS Metric"
                }
            ]
        }    
        ]
}

RequiredJson

[
    {
        "webshop": [
            {
                "id": "1168190",
               "type": "segment",
"name": [
                {
                    "en": "Irrigation"
                },
                {
                    "es": "Riego"
                }
            ],
"values": [ { "id": "1168191","type": "application",
"name": [
                {
                    "ES": "Kenadrain"
                },
                {
                    "EN": "Kenadrain"
                }
            ],
"values": [ { "id": "1168192",
"type": "productRange",
"name": [
                {
                    "ES": "Fluxol"
                },
                {
                    "EN": "Fluxol"
                }
            ].....