Search code examples
jsongroovysoapuijsonslurper

How to parse Json response and truncate child nodes


This is the JSON response I am trying to parse:

{
"data": {
    "Content": {
        "id": 26,
        "name": "Dashboard1"
    },
    "List": [
        {
            "ListContent": {
                "id": 178,
                "name": "Card-144"
            },
            "cards": [
                {
                    "id": 1780,
                    "configuration": {
                        "id": 7178,
                        "name": "Emp"
                    }
                }
            ]
        },
        {
            "ListContent": {
                "id": 179,
                "name": "Card-14"
            },
            "cards": [
                {
                    "id": 1798,
                    "configuration": {
                        "id": 1789,
                        "name": "RandomColumns"
                    }
                }
            ]
        },
        {
            "ListContent": {
                "id": 180,
                "name": "Card-1"
            },
            "cards": [
                {
                    "id": 18080,
                    "configuration": {
                        "id": 1080,
                        "allow": true
                    }
                }
            ]
        },
        {
            "ListContent": {
                "id": 181,
                "name": "Card-14"
            },
            "cards": [
                {
                    "id": 18081,
                    "configuration": {
                        "id": 1881,
                        "name": "Functions"
                    }
                }
            ]
        },
        {
            "ListContent": {
                "id": 182,
                "name": "Card-1443"
            },
            "cards": [
                {
                    "id": 1782,
                    "configuration": {
                        "id": 1802,
                        "name": "Emp-O"
                    }
                }
            ]
        }
    ]
}

}

From the Json, I need to extract "id"s under the "ListContent" nodes and store it in an array. Also, will need to ignore "id"s under the child nodes. Here is a groovy script I am trying to achieve this with,

    def CList = ""
    import groovy.json.JsonSlurper
    def jsonRespData = context.expand( '${TestStep#Response#$.data.List}' ) 
    def outputResp = new JsonSlurper().parseText(jsonRespData)


    outputResp.id.each()
    {log.info( ":"+ it) 
    CList=CList.concat(it.toString()).concat(',')} 


      log.info (CList)

So, the array that I am expecting is CList [178,179,180,181,182] but I am currently getting null. What should be the correct groovy to only read "id" from "ListContent" and write it to an array? Any help would be really appreciated. Thanks in advance.


Solution

  • You can just use the (implicit) spread operator like this:

    def json = new groovy.json.JsonSlurper().parse('/tmp/x.json' as File)
    
    // 
    def i = json.data.List.ListContent.id
    assert i == [178, 179, 180, 181, 182]
    
    // with explicit spread operator
    def e = json.data.List*.ListContent*.id
    assert e == [178, 179, 180, 181, 182]