7

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)
InputJson.each
{
return[
key
]
}

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

3
  • 1
    Help what, exactly? Please learn How to Ask. Commented Sep 29, 2016 at 20:28
  • @jonrsharpe First line of my question states "I need some help in reading a JSON file." That is help i need. Commented Sep 29, 2016 at 21:04
  • 1
    Needing help isn't a question. Commented Sep 29, 2016 at 21:56

2 Answers 2

5

Anyone in similar situation as me trying to import a JSON file at runtime. I used Active Choice parameter to solve my problem. There is an option to write groovy script in Active Choice Parameter plugin of Jenkins. There i have written below code to import a JSON file to achieve desired results.

import groovy.json.JsonSlurper
def inputFile = new File('.//TestSuitesJ.json')
def inputJSON = new JsonSlurper().parse(inputFile)
def keys = inputJSON.keySet() as List

Thanks @sensei to help me learn groovy.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

11 Comments

I tried this but still now working import groovy.json.JsonSlurper def inputFile = new File(".\TestSuitesJ.json") def InputJSON = new JsonSlurper().parseText(inputFile) InputJson.each { k,v -> println k }
You do realize you haven't given us the output or the error you're getting ? Since I'm left to guess, I'd say the fact that you're trying to parse a File instance with the parseText() method (which is for parsing strings) is the first of your problems. And here is an example you can run in the groovy web console that shows it works: groovyconsole.appspot.com/script/5124166701285376
Thanks. Code you gave is printing both key and value. I want only key to print in this case. It should just print smoke and default.
Wow. You still haven't read the groovy doc I pointed to. The code I gave only prints keys, as documented. You've been looking at the "result" tab instead of the "output" tab in the web console. I'm usually glad to help, but this is getting silly.
I know it get irritating sometimes but as i mentioned i have just started groovy today.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.