Karate API Testing - Converting from JSON array to String

2.8k views Asked by At

I need to read an id from a GET request and use that in POST for performing a test. The id that I retrieve from GET is in the form of array from the following expression:

And def reqId =  response.teams[*].resource[1].resourceRequestId

`["59aeb24be4b0b17227553d07"]`

I store this Id as variable:

And def reqId =  response.teams[*].resource[1].resourceRequestId

When I use this in my POST operation, I get the following error:

exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read document: Can not deserialize instance of java.lang.String out of START_ARRAY token\n

Is there a way I can perform JSON.stringify() etc to convert the value to a string or a way to extract it and use it more purposefully in POST request

1

There are 1 answers

0
Peter Thomas On BEST ANSWER

You are sending an array instead of a single string. Keep in mind that the moment you use [*] in a JsonPath, the result is always an array.

And def temp =  response.teams[*].resource[1].resourceRequestId
And def reqId = temp[0]

Now try, it should work. We are introducing a get[0] syntax convenience to make this cleaner in the next version. Look out for it.