I try to include a dynamic array in a json variable and it fails.
hostsArray is dynamically built
# sample of hostsArray
* def hostsArray = '[{"hostid": "1234"},{"hostid": "4567"}, {"hostid": "9865"}]'
* def myjson =
"""
{
"key1": "val1",
"params" : {
"key2": "val2",
"hosts" : #(hostsArray)
}
"""
expected json:
{
"key1": "val1",
"params" : {
"key2": "val2",
"hosts" : [
{"hostid": "1234"},
{"hostid": "4567"},
{"hostid": "9865"}
]
}
I always got an error or a string, when using '#(hostsArray)' instead of the array structure
Is there a way to do that?
Thanks
I finally found a way to do that. Thanks to Rajeev KR answer which drive me to the solution
The hostArray string has to be converted to JSON and it can be done using with a type conversion to json https://github.com/karatelabs/karate#type-conversion
# sample of hostsArray
* def hostsArray = '[{"hostid": "1234"},{"hostid": "4567"}, {"hostid": "9865"}]'
* json hostArrayJson = hostsArray
* def myjson =
"""
{
"key1": "val1",
"params" : {
"key2": "val2",
"hosts" : '#(hostsArrayJson)'
}
"""