I'm quite new with scripts. I have been searching quite a while and got stuck at this point. I'm trying to create a json array at Jmeter JSR223 PreProcessor using variables from a csv file to post at Jmeter.
The json structure is;
[
{
"id": "${FacId}",
"name": "${Name}",
"type": "Facility"
}
]
I can read the variables from csv and set the id and name as array using;
def builder = new groovy.json.JsonBuilder()
@groovy.transform.Immutable
class BSO{
String id
String name
}
def units = new File("/Users/gulcei/Documents/TPTest/test.csv")
.readLines()
.collect { line ->
new BSO (line.split(",")[0], line.split(",")[1]) }
builder(
units.collect(),
type: "Facility"
)
log.info(builder.toPrettyString())
vars.put("payload", builder.toPrettyString())
But rest of the values stays out of the array and it returns as;
[
{
"type": "Facility"
},
[
{
"id": "id1",
"name": "Facility1"
},
{
"id": "id2",
"name": "Facility2"
}
]
]
And one more thing; if i try to add a value with two components as;
[
{
"id": "${FacId}",
"name": "${Name}",
"type": "Facility",
"location": {
"latitude": 0,
"longitude": 0
}
}
]
i get an "unexpected token" error. I think i'm trying to put it in the wrong format.
Can you please help me deal with it.
Thanks in advance...