Forum Discussion

maddenapally's avatar
5 years ago

How to add a field to JSON Request using Groovy Script.



I have scenario like I need to add highlighted json field to Request. Can anyone please help me in this.









  • nmrao's avatar
    5 years ago
    Here is the sudo steps to achieve the same.
    Write a script (you can add it in a temporary suite/test case.
    1. You might know to which test step the request needs to be updated based on its service or Method
    2. Based on that, define all method names into a list.
    3. Iterate thru all the test cases and if particular request method is found in your list then do the replacement (which you already have the logic)

    Once the requests are updated, remove that temporary suite/test case.

    So, single script can handle to update and you don't need to add the script in each and every test case.

10 Replies

  • Use JsonSlurper and JsonBuilder, they are Groovy classes for working with JSON and are already available in SoapUI.


    import groovy.json.*
    def request = new JsonSlurper().parseText(<your request JSON here>) // Deserialize JSON request to an object
    request.putAt("generationTime", "2016-01-26T16:07:07Z") // Add your new field
    def requestStr = new JsonBuilder(request).toString() // or toPrettyString(); this serializes the object back to JSON
    • maddenapally's avatar

      Thanks JustinM89,


      I have 200 Test cases, That's have differenent types of test data. How can I handle this, What I am looking for is

      I have to write a script that has to update all the request, with generation time field,




      • JustinM89's avatar

        So you need to do this in 200 separate test cases? I don't know of a way you could do this without adding this script to all of them. You might be able to make it work using an event listener but it would be a little hacky.