I am new on Groovy, I am trying to mock a service in soap ui.
Requirement is to load a text file with json data, and load a node with matching data.
whati have tried is as below,
def inputFile = new File("D:\\Users\\json.txt")
def InputJSON = new JsonSlurper().parseText(inputFile.text)
InputJSON.each{
def ID1 = it
it.items.each {
if(it.Number == itemNumber)
{
log.info it
requestContext.Id = ID1
}
}
}
This works perfectly fine just one issue, is the format. when ID1 loads into requestContext.Id, the format of json is lost from the file.
What I need is
{
"items" {
"number" : 1475175072691
}
}
what i get is
{
metadata = {
timestamp = 1475175072691
}
}
Why does it take away the double quotes " and colon : from my json. Please suggest.
The following code:
import groovy.json.*
def str = '''\
[
{
"items":{
"number":1475175072691
}
},
{
"items":{
"number":1475175072691
}
},
{
"items":{
"number":1475175072691
}
},
{
"items":{
"number":1475175072691
}
}
]'''
def json = new JsonSlurper().parseText(str)
json.each {
println JsonOutput.prettyPrint(JsonOutput.toJson(it))
}
prints out the following result:
{
"items": {
"number": 1475175072691
}
}
{
"items": {
"number": 1475175072691
}
}
{
"items": {
"number": 1475175072691
}
}
{
"items": {
"number": 1475175072691
}
}
In other words, you need to serialize and pretty print the json nodes to get the format you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.