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.

On Monday, April 19, 2021 at 9:23:27 AM UTC-7 jesusfern...@gmail.com wrote:

> I have a json object which I receive in my jenkins pipeline through a url 
> and I parse it like this:
> ```
> def url = 'http://localhost:8080/job/find_issues/22/cppcheck/all/api/json'
> 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...
>

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/93d32032-bf4d-4889-9377-7c7f1f39d322n%40googlegroups.com.

Reply via email to