Ask a Question

Create json in Groovy

SOLVED
henil_shah
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"

}

10 REPLIES 10
nmrao
Community Hero

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.
Olga_T
SmartBear Alumni (Retired)

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

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

@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.
Olga_T
SmartBear Alumni (Retired)

Thank you, @nmrao .

@henil_shah , does the above suggestion help?

 


Olga Terentieva
SmartBear Assistant Community Manager

nmrao
Community Hero

@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.
Olga_T
SmartBear Alumni (Retired)

@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

Olga_T
SmartBear Alumni (Retired)

I guess we could mark Rao's reply as a solution for now.

@henil_shah feel free to post any updates here if you would like to find alternative ways to solve this.

 

Best regards,

 


Olga Terentieva
SmartBear Assistant Community Manager

@Olga_T - Sometimes, before spamming you should understand that someone might be on a vacation and have no access to reply. Please wait for sometime for OP to respond. As far as the solution, its good and thanks @nmrao for the solution.

cancel
Showing results for 
Search instead for 
Did you mean: