Forum Discussion

forkbattery's avatar
forkbattery
Occasional Contributor
11 years ago

Read file with JSON content and use them in requests?

Hi,

 

Is there a way to read an external file with a number of JSON requests and then run the test case in a loop passing in the JSON document one by one with a for loop, using Groovy?

 

Any help would be appreciated!

 

Thanks

1 Reply

  • forkbattery's avatar
    forkbattery
    Occasional Contributor

    I figured out the following to read file using JsonSlurper. I'm able to print each JSON document with the help of 'each' as seen in the code below. I can't figure out how I would pass each of the documents into the request of a test case. I'd really appreciate if anyone could tell me how this can be done!

     

    import groovy.json.*
    File f = new File('C:\\contacts.txt')
    def slurper = new JsonSlurper()
    def jsonText = f.getText()
    json = slurper.parseText( jsonText )
    json.each{
    	log.info it
    }