Groovy从复杂的json结构中收集和删除重复项

xuo3flqw  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(141)

这是我在Stack Overflow的第一个问题,所以,首先,各位同事好,非常感谢。
我有这个json输入消息,我正在处理,但是我找不到键来获得我需要进一步处理的消息

{
  "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
}

对于已知的ID(“Payer_161185”或“ShiptoInc_161185”)和给定的值(“100563692”),我们需要提取所有UID的www.example.com的所有重复项businessEntities.codes,并在获得列表后删除重复项。
例如,对于“ShiptoInc_161185”,所需的输出将是:

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

此输出是将salesAreas列表中的给定值100563692转换为所有id = ShiptoInc_161185
我想解决的另一个案例是:
我如何添加id而不是文本salesAreas?类似于{“Payer_111”:[“HU50_HU50_10_10”,“HU50_HU50_30_20”],“Payer_222”:[“HU40_HU40_10_10”,“HU20_HU20_30_20”]}。这意味着不会提供id,只提供前缀Payer_
感谢您的帮助。

***我解决了第二个问题

def data = new JsonSlurper().parseText(body);

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

    def nList = [];
    def rembM = bModelsIdFiltered.each{
        it.businessEntities.codes.each { code ->
            nList.add(code.plus(it.id))
        }
    }

    println "nl " + nList;

    def codesFiltered = nList
            .findAll { '100563692' in it }

    return codesFiltered;
xam8gpfp

xam8gpfp1#

如果结构是刚性的,并且我正确地理解了任务(即,你应该在同一个数组中找到值为100563692的代码),你可以这样做:

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')
    }
}

相关问题