Need help to read a json file using groovy and Jenkins

19,487

Solution 1

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.

Solution 2

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.

Share:
19,487
NewWorld
Author by

NewWorld

Updated on August 20, 2022

Comments

  • NewWorld
    NewWorld over 1 year

    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?

  • NewWorld
    NewWorld over 7 years
    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 }
  • sensei
    sensei over 7 years
    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
  • NewWorld
    NewWorld over 7 years
    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.
  • sensei
    sensei over 7 years
    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.
  • NewWorld
    NewWorld over 7 years
    Got it. Thanks.
  • NewWorld
    NewWorld over 7 years
    I know it get irritating sometimes but as i mentioned i have just started groovy today.
  • NewWorld
    NewWorld over 7 years
    If you can, can you please help me what am i missing in below code? import groovy.json.JsonSlurper def inputFile = new BufferedReader(new FileReader(".\\TestSuitesJ.json")) def InputJSON = new groovy.json.JsonSlurper().parse(inputFile) def List = new ArrayList() InputJson.each {k,v -> list.add(k) } return list I can't tell you error as i am using this in Jenkins. I am not getting any error there but not getting any output too.
  • sensei
    sensei over 7 years
    I don't see where you mentionned that, but now knowing something is alright. However, when you're given pointers to documentation, it is indeed irritating when you answer "it doesn't work" without having read and understood said documentation (and, pretty please, do always paste the error you're getting).Anyways, thanks for validating the answer.
  • sensei
    sensei over 7 years
  • NewWorld
    NewWorld over 7 years
    I tried with code below: import groovy.json.JsonSlurper def inputFile = new File('.//TestSuitesJ.json') def inputJSON = new JsonSlurper().parse(inputFile) This is giving me value in dropdown: Test1.js and Test2.js return [inputJSON] is giving me any error. Any idea what else can i use?
  • NewWorld
    NewWorld over 7 years
    Below code work in same way. This is also giving me value and not keys. import groovy.json.JsonSlurper def inputFile = new File('.//TestSuitesJ.json') def inputJSON = new JsonSlurper().parse(inputFile) return inputJSON Below code is also giving me values instead of key import groovy.json.JsonSlurper def inputFile = new File('.//TestSuitesJ.json') def inputJSON = new JsonSlurper().parse(inputFile) inputJSON.each {k,v -> k}
  • NewWorld
    NewWorld over 7 years
    Finally got this to work. Thank you for your help. Below code worked fine import groovy.json.JsonSlurper def inputFile = new File('.//TestSuitesJ.json') def inputJSON = new JsonSlurper().parse(inputFile) def keys = inputJSON.keySet() as List