NewWorld NewWorld - 22 days ago 17x
Groovy Question

Need help to read a json file using groovy and Jenkins

I am facing some issues reading a JSON file.
I am using Jenkins Active Choice Parameter to read value from a JSON file via groovy script. This is how my JSON file look.

"smoke": "Test1.js",
"default": "Test2.js"

I want my groovy script to print out smoke and default. Below is what my groovy code look like.

import groovy.json.JsonSlurper
def inputFile = new File(".\TestSuitesJ.json")
def InputJSON = new JsonSlurper().parseText(inputFile)

Above code is not working for me. Can someone please suggest a better groovy way?


You really should read the groovy dev kit page, and this in particular.

Because in your case parseText() returns a LazyMap instance, the it variable you're getting in your each closure represents a Map.Entry instance. So you could println it.key to get what you want.

A more groovy way would be:

inputJson.each { k, v ->
  println k

In which case groovy passes your closure the key (k) and the value (v) for every element in the map.