Home > OS >  Groovy to collect and remove duplicates from a complex json structure
Groovy to collect and remove duplicates from a complex json structure

Time:08-02

this is my first question at Stack Overflow, so, firstly, Hello colleagues and many thanks in advance.

I have this json input message I'm dealing with, but I cannot find the key to get the message I need for further processing

    {
  "callId": "70f354ed47e643bc9d1cd6595e018f9b",
  "errorCode": 0,
  "apiVersion": 2,
  "statusCode": 200,
  "statusReason": "OK",
  "time": "2022-08-01T07:56:34.631Z",
  "results": [
    {
      "UID": "5abc8d08d8e148158610c7c6776c4ad5",
      "groups": {
        "organizations": [
          {
            "businessModels": [
              {
                "keys": [
                  "Company Code",
                  "Sales Org",
                  "Distribution Channel",
                  "Division"
                ],
                "businessEntities": [
                  {
                    "codes": [
                      "HU50",
                      "HU50_HU50",
                      "HU50_HU50_10",
                      "HU50_HU50_10_10"
                    ]
                  }
                ],
                "id": "SalesArea_161185"
              },
              {
                "keys": [
                  "ShiptoInc_SalesArea",
                  "ShiptoInc_Id"
                ],
                "businessEntities": [
                  {
                    "codes": [
                      "HU50_HU50_10_10",
                      "100563692"
                    ]
                  },
                  {
                    "codes": [
                      "HU50_HU50_10_10",
                      "100563691"
                    ]
                  }
                ],
                "id": "ShiptoInc_161185"
              },
              {
                "keys": [
                  "Payer_SalesArea",
                  "Payer_Id"
                ],
                "businessEntities": [
                  {
                    "codes": [
                      "HU50_HU50_10_10",
                      "960004763"
                    ]
                  }
                ],
                "id": "Payer_161185"
              }
            ]
          }
        ]
      }
    },
    {
      "UID": "d9f2b591f58e4aeebaa0b88175d4fe3c",
      "groups": {
        "organizations": [
          {
            "businessModels": [
              {
                "keys": [
                  "Company Code",
                  "Sales Org",
                  "Distribution Channel",
                  "Division"
                ],
                "businessEntities": [
                  {
                    "codes": [
                      "HU50",
                      "HU50_HU50",
                      "HU50_HU50_10",
                      "HU50_HU50_10_10"
                    ]
                  }
                ],
                "id": "SalesArea_161185"
              },
              {
                "keys": [
                  "ShiptoInc_SalesArea",
                  "ShiptoInc_Id"
                ],
                "businessEntities": [
                  {
                    "codes": [
                      "HU50_HU50_10_10",
                      "100563692"
                    ]
                  },
                  {
                    "codes": [
                      "HU50_HU50_10_10",
                      "100563691"
                    ]
                  }
                ],
                "id": "ShiptoInc_161185"
              },
              {
                "keys": [
                  "Payer_SalesArea",
                  "Payer_Id"
                ],
                "businessEntities": [
                  {
                    "codes": [
                      "HU50_HU50_10_10",
                      "960004763"
                    ]
                  }
                ],
                "id": "Payer_161185"
              }
            ]
          }
        ]
      }
    },
    {
      "UID": "74a9ccbc9b8549d1a7726ac1f77f7ea9",
      "groups": {
        "organizations": [
          {
            "businessModels": [
              {
                "keys": [
                  "ShiptoInc_SalesArea",
                  "ShiptoInc_Id"
                ],
                "businessEntities": [
                  {
                    "codes": [
                      "HU50_HU50_10_10",
                      "100563692"
                    ]
                  }
                ],
                "id": "ShiptoInc_161185"
              }
            ]
          }
        ]
      }
    },
    {
      "UID": "d5ed356a3c2a48568ccacb8d9c7c5506",
      "groups": {
        "organizations": [
          {
            "businessModels": [
              {
                "keys": [
                  "Company Code",
                  "Sales Org",
                  "Distribution Channel",
                  "Division"
                ],
                "businessEntities": [
                  {
                    "codes": [
                      "HU50",
                      "HU50_HU50",
                      "HU50_HU50_10",
                      "HU50_HU50_10_10"
                    ]
                  }
                ],
                "id": "SalesArea_161185"
              },
              {
                "keys": [
                  "ShiptoInc_SalesArea",
                  "ShiptoInc_Id"
                ],
                "businessEntities": [
                  {
                    "codes": [
                      "HU50_HU50_10_10",
                      "100563692"
                    ]
                  },
                                    {
                    "codes": [
                      "HU50_HU50_20_20",
                      "100563692"
                    ]
                  },
                  {
                    "codes": [
                      "HU50_HU50_10_10",
                      "100563691"
                    ]
                  }
                ],
                "id": "ShiptoInc_161185"
              },
              {
                "keys": [
                  "Payer_SalesArea",
                  "Payer_Id"
                ],
                "businessEntities": [
                  {
                    "codes": [
                      "HU50_HU50_10_10",
                      "960004763"
                    ]
                  }
                ],
                "id": "Payer_161185"
              }
            ]
          }
        ]
      }
    }
  ],
  "objectsCount": 4,
  "totalCount": 4
}

For a known id ("Payer_161185" or "ShiptoInc_161185") and a given value ("100563692") we need to extract all repetitions of businessEntities.codes of all UIDs and after get the list, remove duplicates.

For example, for "ShiptoInc_161185", the desired output would be:

{ "salesAreas": ["HU50_HU50_10_10","HU50_HU50_20_20"]}

This output is the list of salesAreas for the given value 100563692 into all the id = ShiptoInc_161185

Your help is appreciated.

CodePudding user response:

If the structure is rigid and i understood the task correctly (namely, you should find codes that have value 100563692 in the same array), you can doing it like that:

class FindCodesSpec extends Specification {
    def testString = '''<insert_your_string_here>'''

    def flattenOnce(List array) {
        return array.inject([]) { res, el -> res   el }
    }

    def findCodes(String message, String id, String code) {
        def data = new JsonSlurper().parseText(message)

        def bModelsIdFiltered = data.results.groups.organizations.businessModels
                .collect { it[0] }.flatten()
                .findAll { it.id == id }

        def codesFiltered = flattenOnce(bModelsIdFiltered.businessEntities.codes)
                .findAll { code in it }

        def uniqueCodes = codesFiltered.flatten().unique() - code
        return JsonOutput.toJson(['salesAreas': uniqueCodes])
    }

    def 'run test'() {
        expect:
        '''{"salesAreas":["HU50_HU50_10_10","HU50_HU50_20_20"]}''' == findCodes(testString, 'ShiptoInc_161185', '100563692')
    }
}

  • Related