cancel
Showing results for 
Search instead for 
Did you mean: 

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

Contributor

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

Team,

 

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

 

{
"lastMessage":${Properties#lastMessage_F},
"generationTime":"2016-01-26T16:07:07Z",

}

 

Regards,

Karthik

 

 

9 REPLIES 9
Contributor

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

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
Contributor

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

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,

 

Regards,

Karthik

Contributor

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

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.

Contributor

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

Is there any way to do getting JSON path, Setting the field value over there.

 

Regards,

Karthik

Community Hero

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

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.


Regards,
Rao.
Contributor

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

Hi nmrao,

 

Thanks for the help, Actually what is the scenario is, Dev team added extra field to the RQ, We have test cases 12, test steps are more than 1500, It is difficult to update each Test step to the test case,  That's why I want to Automate this process. Please help me in this.

 

Here is the steps for scenario,

1. I have a test case in that I have 1500 test steps(RQ).

2. I need to add the Generation time field to each RQ.

3. Is there any way to display in the RQ that field.

 

Thanks in Adavance.

 

Contributor

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

Any update on this?

 

Regards,

Karthik

Highlighted
Community Hero

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

Please understand that people helping here from the community and people like you; not from support team.
Procedure has been mentioned in previous reply. Were you able to make any progress based on that? Where do you have problem?


Regards,
Rao.
Moderator

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

Thank you for your replies, everyone!

@maddenapally, have you managed to find a solution? If not, could you please describe the stage you are on now?

 

Olga Terentieva,
SmartBear Assistant Community Manager

New Here?
Join us and watch the welcome video:
Top Kudoed Authors