Search code examples
jsongroovynestedcollect

Groovy collect nested elements along with outer element


Using Groovy, requirement is to collect a map's nested element values, along with its top-level element value.

Unsure if a recursive method is needed.

Sample JSON

{
"items": [
      {
      "attribute": "Type",
      "options":       
      [
                  {
            "label": "Type1",
            "value": "1"
         },
                  {
            "label": "Type2",
            "value": "2"
         }

      ]
   },
      {
      "attribute": "Size",
      "options":       [
                  {
            "label": "SizeA",
            "value": "A"
         },
                  {
            "label": "SizeB",
            "value": "B"
         }
      ]
   }
]
}

Expected output after collect

[
{attribute=Type,label=Type1,value=1},
{attribute=Type,label=Type2,value=2},
{attribute=Size,label=SizeA,value=A},
{attribute=Size,label=SizeB,value=B}
]

Solution

  • You can solve this combining multiple lists of options obtained through the collect method using a collectMany.

    See the following snippet of code:

    def input = """
    {
      "items": [
        {
          "attribute": "Type",
          "options": [
             {
               "label": "Type1",
               "value": "1"
             },
             {
               "label": "Type2",
               "value": "2"
             }
          ]
       },
       {
         "attribute": "Size",
         "options": [
             {
                "label": "SizeA",
                "value": "A"
             },
             {
                "label": "SizeB",
                "value": "B"
             }
         ]
      } ]
    }"""
    
    def json = new groovy.json.JsonSlurper().parseText(input)
    
    /* collectMany directly flatten the two sublists 
     * [ [ [ type1 ], [ type2 ] ], [ [ sizeA ], [ sizeB ] ] ]
     * into
     * [ [type1], [type2], [sizeA], [sizeB] ]
     */
    def result = json.items.collectMany { item ->
        // collect returns a list of N (in this example, N=2) elements 
        // [ [ attribute1: ..., label1: ..., value1: ... ],
        //   [ attribute2: ..., label2: ..., value2: ... ] ]
        item.options.collect { option ->
            // return in [ attribute: ..., label: ..., value: ... ]
            [ attribute: item.attribute ] + option
        }
    }
    
    assert result == [
        [ attribute: "Type", label: "Type1", value: "1" ],
        [ attribute: "Type", label: "Type2", value: "2" ],
        [ attribute: "Size", label: "SizeA", value: "A" ],
        [ attribute: "Size", label: "SizeB", value: "B" ],
    ]