I have below JSON output from AWS DynamoDB and I want to iterate through it and populate the table in Jenkins Parameters through groovy script. Is it possible to do?
JSON:
[
{
"test": {
"S": "0"
},
"dev": {
"S": "1"
},
"cert": {
"S": "2"
},
"prod": {
"S": "3"
}
}
]
I tried to iterate through the list and I could not get the value from "S": "1"
Groovy Code:
import groovy.json.JsonSlurper
def json = '''
[
{
"test": {
"S": "0"
},
"dev": {
"S": "1"
},
"cert": {
"S": "2"
},
"prod": {
"S": "-3"
}
}
]
'''
def json_out = new JsonSlurper().parseText(json)
json_out.each{ key, value ->
value.each {k, v ->
println "${key}"
println "${v}"
}
}
Error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 3: expecting '}', found ':' @ line 3, column 15.
"test: {
^
Your json is a list of objects:
import groovy.json.JsonSlurper
def myMaps = '''
[
{
"test": {
"S": "0"
},
"dev": {
"S": "1"
},
"cert": {
"S": "2"
},
"prod": {
"S": "3"
}
}
]
'''
def jsonMap = new JsonSlurper().parseText(myMaps)
jsonMap.each { obj ->
obj.each { key, value ->
println key
println value.S
}
}