cancel
Showing results for 
Search instead for 
Did you mean: 

Create json in Groovy

Contributor

Create json in Groovy

Need to create Json like this

"data":

{

"data1":"data1",

"data2":"data2"

}

}

 

I'm able to create the below one but not the above one. I'm using def json = new JsonOutput().toJson(obj) for the below

{

"data1":"data1",

"data2":"data2"

}

7 REPLIES 7
Highlighted
Community Hero

Re: Create json in Groovy

Creating that would not be difficult. What I am curious is more context to the problem. How are you getting the input data and how it is going to be used.


Regards,
Rao.
Moderator

Re: Create json in Groovy

Hi all,

 

@henil_shah, have you managed to accomplish the task?

If you haven't, could you please provide the information that @nmrao requested?

Thanks in advance.

 

Olga Terentieva,
SmartBear Assistant Community Manager

Contributor

Re: Create json in Groovy

Input data will be in Groovy script with random data provided to a test case. I'm able to create simple Json but not this kind of Json

Community Hero

Re: Create json in Groovy

@henil_shah 

 

Would you please check this script 

You may test it online in the above link.

import static groovy.json.JsonOutput.toJson
import static groovy.json.JsonOutput.prettyPrint
def data = [data1: "data1", data2: "data2"]
def jsonString = prettyPrint(toJson(data: data).toString())
log.info jsonString


Regards,
Rao.
Moderator

Re: Create json in Groovy

Thank you, @nmrao .

@henil_shah , does the above suggestion help?

 

Olga Terentieva,
SmartBear Assistant Community Manager

Community Hero

Re: Create json in Groovy

@Olga_T,
Some time members float the thread with messages. And do not reply once the solution is posted. There could be multiple reasons for being delayed in reply. But, it would be encouraging if reply is prompt on the solutions or providing with feedback.

 

This message is not just specific to this case, it is the case in general. I do notice that it happens even the members are online, no reply after the solution recieved.

Thank you for taking a look.



Regards,
Rao.
Moderator

Re: Create json in Groovy

@nmrao , I agree. I would be happy if this helps find a solution faster. 
Some users looking for information in the existing topics prefer going through only those topics that are marked as solved. So, this can increase the chances for them to find useful information. 
I hope to see this topic solved soon Smiley Happy


Regards,

Olga Terentieva,
SmartBear Assistant Community Manager

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