Save More Than Value From JSON

Hi,

I want to extract 2 properties from a random JSON object in an array for use in subsequent http calls. How can I do that?

Let’s say the JSON response is this:

`
[
{ “field1”: 1,
“field2”: 2,
“field3”: 3
},
{
“field1”: 4,
“field2”: 5,
“field3”: 6
}
]

`

And I need to extract field1 and field2 from a random object and put them in the session. I’ve tried:

`

.check(jsonPath("$[*][‘field1’, ‘field2’]").findRandom.saveAs(“row”)))

`

When I do this, only the value of field1 is saved to “row”.

Alternatively, I suppose I could store the whole object in row, but I’m not clear how to extract the fields I’m interested in afterwards.

Any ideas?

Thanks!

Charles

Hi Charles,

I had a similar problem in past. I also had a problem of bulkey logs when the response is not in the json format when I use jsonPath(). I moved into using regex to sort that out.
I use .SaveAs() to save the values into session like below.

.check(regex("""“f_name”:\s*"([^"]*)""").saveAs(“firstName”))

Hope this helps.

-Pujitha.

Hi Pujitha,

Thanks for the reply… Your example only shows saving one value, but I guess I could capture two groups and save them as a tuple.

I ended up just doing two jsonPath statements, because I find the regex hard to read. Because one of the fields was an id and unique (which I didn’t show in my simplified example in my original post), I could use the value of the first id in the second statement:

`
.check(jsonPath("$[*].id").findRandom.saveAs(“id”))
.check(jsonPath("$[?(@.id == ${id})].another_field").saveAs(“another_field”)

`

This may not be the most efficient way, but it works.

Thanks,

Charles

Thanks that worked for me with UUID example:

`

.check(jsonPath("$.result.assetDetails[*].id").findRandom.saveAs(“qnaUUID”))
.check(jsonPath("$.result.assetDetails[?(@.id == “${qnaUUID}”)].metadata[0].value").saveAs(“assUUID”)))

`