how can I iterate a json with a list of objects in a pipeline?

799 views
Skip to first unread message

jesus fernandez

unread,
Apr 19, 2021, 12:23:27 PM4/19/21
to Jenkins Users
I have a json object which I receive in my jenkins pipeline through a url and I parse it like this:
```
def json = new JsonSlurper().parseText( new URL(url).text )

```

Which has this format
```
{
"_class": "io.jenkins.plugins.analysis.core.restapi.ReportApi",
"issues": [],
"size": 100,
"toString": "- (-): 100 issues (0 duplicates)"
}
```
which can have a different number of issues inside the `issues` list, each of them having this structure:
```
{
"addedAt": 0,
"authorEmail": "-",
"authorName": "-",
"baseName": "stat_obj.cpp",
"category": "",
"columnEnd": 0,
"columnStart": 0,
"commit": "-",
"description": "",
"fileName": "D:/Source/Game/objects/stat_obj.cpp",
"fingerprint": "D2CD3A23FB45D3A1F1C3CB8BE5241602",
"lineEnd": 5592,
"lineStart": 5592,
"message": "Unmatched '('. Configuration: 'DEBUGMAPEXPLORATION'.",
"moduleName": "",
"origin": "cppcheck",
"originName": "CPPCheck",
"packageName": "-",
"reference": "19",
"severity": "HIGH",
"toString": "D:/Source/Game/objects/stat_obj.cpp(5592,0): syntaxError: : Unmatched '('. Configuration: 'DEBUGMAPEXPLORATION'.",
"type": "syntaxError"
}
```
How can I access the Issues list and the iterate through each one of the issues and them through each of the issue keys? 
I have tried different ways, lastly I have this:
```
for (entry in json) {
    if(entry.key == "issues")
    {
        for (issue in entry.value) {
            entry.value.each{ key, value ->
                if (key == "fileName"){
                    errorsList.add(value)
                }
                if (key == "lineStart"){
                    errorsList.add(value)
                }
                if (key == "message"){
                    errorsList.add(value)
                }
                if (key == "severity"){
                    errorsList.add(value)
                }
                if (key == "type"){
                    errorsList.add(value)
                }
                def msg = "New ERROR found in static analysis, TYPE OF ERROR ${errorsList[4]}, SEVERITY: ${errorsList[3]}, ERROR MESSAGE: ${errorsList[2]}, FILE ${errorsList[0]} AT LINE: ${errorsList[1]}"
                println msg
                errorsList.clear()
            }
        }
    }
}
```
But when I call print `msg` I have something like this:
```
17:13:09  New ERROR found in static analysis, TYPE OF ERROR null, SEVERITY: null, ERROR MESSAGE: null, FILE null AT LINE: null
```
It seems like I am over iterating but I do not get to see where is the mistake...

David Karr

unread,
Apr 19, 2021, 12:55:19 PM4/19/21
to Jenkins Users
You're missing the most important value of what JsonSlurper (and similar tools) provides. There is no reason to iterate maps, and it's important to realize that there is no ordering guarantee of map values.

If you want to reference the "issues" property, just reference "json.issues". You will need to iterate over the "json.issues" list, and each of those objects should be "issue" objects, and if you need to reference those particular properties, just reference "issue.type", "issue.severity", et cetera.

jesus fernandez

unread,
Apr 19, 2021, 1:58:32 PM4/19/21
to Jenkins Users
Thanks it works now!
Reply all
Reply to author
Forward
0 new messages