Hello I previously asked how to dynamically generate a json array using Groovy here:
JMETER – Populate a JSON request for all items in an array
The provided solution worked great, however I need to add an additional object after the final item in the array. Because the toprettystring formats the array I am unsure how to insert the object into the final result.
Here is an example of the final JSON:
{
"books": [
{
"publisher": ${publisherName}, //variable but it's the same for all items in this request
"ISBN": ${isbn_1}, //the next three are from from 3 different arrays
"Price": ${price_1},
"Publisher": ${publisher_1},
"Static Value that doesn't change": 100
},
{
"publisher": ${publisherName}, //variable but it's the same for all items in this request
"ISBN": ${isbn_2}, //the next three are from from 3 different arrays
"Price": ${price_2},
"Publisher": ${publisher_2},
"Static Value that will be the same for all items": 100
},
],
"meta": {
"tag": {
"searchNumber": ${searchNo}
}
}
}
The solution for the previous question was to use a JSR223 Preprocessor and generate it as follows:
def arraySize = vars.entrySet().findAll { entry -> entry.getKey().startsWith('isbn_') }.size()
def books = []
1.upto(arraySize, {index ->
books.add([publisher:vars.get('publisherName'),
ISBN: vars.get('isbn_' + index),
Price: vars.get('price_' + index),
Publisher:vars.get('publisher_' + index),
"Static Value that doesn't change": 100
])
})
log.info(new groovy.json.JsonBuilder([books:books]).toPrettyString())
How could I modify this to enter the above object into the final JSON?
2
Answers
your final result formed here:
just add required fields into this map:
[books:books]
for example instead of last line:
Amend your code as follows:
More information: