Parse JSON object in Jenkins

Joel

I'm having some problems with parsing this json file. Seems like i'm having some problems with parsing the object-keyname and I'm not quite sure how to solve this.

My JenkinsfileProperties.json file looks like this:

{
   "predefines": {
        "69dA06x01": {
            "customer": "hello1",
            "label":    "test1",
            "opt":      true,
            "license": "baseline"
        },
        "69dR08x06": {
            "customer": "hello2",
            "label":    "test2",
            "opt":      true,
            "license": "baseline"
        }
    }
}

My groovy file looks like this:

conf = getJobConfiguration scm: scm, local: "checkout", file: "JenkinsfileProperties.json"

conf.predefines.each { predef ->
    builds["Build ${predef}"] = build(predef)
    lints["Lint ${predef}"] = lint(predef)
    unitTests["Unit Test ${predef}"] = unitTest(predef, predef.customer)
}

In my head config.predefines.each { predef -> will give me each instance of 69d.... along with its children. So accessing a child is simply the matter of doing predef.customer or predef.label etc.

Right now i get No such field found: field java.util.AbstractMap$SimpleImmutableEntry customer. What am I doing wrong?

I need to be able to iterate through the 69... entries and I will also have to be able to get their value i.e. 69dA06x01

Joel

Solved it like this

Key values (69d...) are accessed like this: predef.key

The keys subentries are accessed like this:

predef.value["customer"]
predef.value["opt"]
predef.value["license"]
predef.value["label"]

All together:

conf.predefines.each { predef ->
    builds["Build ${predef.key}"] = build(predef)
    lints["Lint ${predef.key}"] = lint(predef)
    unitTests["Unit Test ${predef.key}"] = unitTest(predef, predef.value["customer"])
}

This works if the sub-entries are the same all the way, but i might see a scenario where all subentries are called different things in each 69d... object. Fortunately this works for this scenario.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related