JSON parsing problem

Hi,

I am trying to parse JSON returned from a request in the following format:

{“id”:“1072920417”,“result”:"[{“SearchDefinitionID”:116},{“SearchDefinitionID”:108}]",“error”:null}

I’d like to save the first SearchDefinitionID value to a session variable, and the second to another.

I’ve tried various paths to get the first value with no luck, such as:

.check(jsonPath("/result/SearchDefinitionID").find.saveAs(“firstSearchDefinitionID”))

also
.check(jsonPath("/result/SearchDefinitionID").find(0).saveAs(“searchDefinitionID”))

By the way, if I don’t really care if a value is found - say I try to select the second but there’s only one - to avoid the check failing can I use ‘whatever’ , such as .find.whatever.saveAs…

Thanks,

Greg.

Hi,

The problem is with your JSON message: the “result” attribute contains a whole [{“SearchDefinitionID”:116},{“SearchDefinitionID”:108}] String instead of JSON content.

{“id”:“1072920417”,“result”:"[{“SearchDefinitionID”:116},{“SearchDefinitionID”:108}]",“error”:null} → wrong
{“id”:“1072920417”,“result”:[{“SearchDefinitionID”:116},{“SearchDefinitionID”:108}],“error”:null} → right

I tested the second message with /result/SearchDefinitionID and it works perfectly fine.

Stéphane

Thanks for the reply Stéphane, I’ve been away for the holidays so only got back to this now.

The problem is that I’m being returned the string that I posted, i.e.:

{“id”:“1072920417”,“result”:"[{“SearchDefinitionID”:116},{“SearchDefinitionID”:108}]",“error”:null}

Not only does this parse correctly in client javascript, I tested this on a number of online JSON parsers and each one reported valid JSON.

Obviously the difference compared to the string that works is the double quote escaping. Though the escaping seems unnecessary in this case, unfortunately I don’t have control on how this is formatted.

Is there any way I can clean the response (i.e. replace all occurrences of " with ") before parsing? Otherwise I guess a regular expression might do the job in this case.

Thanks again

Greg.

Hi Greg,

It IS valid JSON, I agree, but /result/SearchDefinitionID is NOT a valid
path for it.

The "result" field value is the whole
"[{\"SearchDefinitionID\":116},{\"SearchDefinitionID\":108}]" *STRING*, not
an array whose JSON serialized form would
be [{"SearchDefinitionID":116},{"SearchDefinitionID":108}]. As a
consequence, SearchDefinitionID is not a field, it's just some characters
inside the string, and you can't reach it with JsonPath.

If you're positively sure that this message is what you expect, you have to
go with a regex check like:

regex(""""SearchDefinitionID\\":(\d*)""").findAll

Cheers,

Stéphane

Stéphane,

Ah, now I understand what you were getting at in your original reply. I hadn’t inspected the returned JSON closely enough to realise the result value was simply a string. Turns out the infrastructure we use returns JSON in a standard format which always includes id, result and error values. If the underlying result is also actually JSON, it returns it as an encoded string in the result parameter, with the upshot being two levels of JSON returned.

So, I have used a regular expression based on your suggestion instead.

Thanks for your help,

Greg.

(sorry about the delayed response, as usual performance testing put on hold when functional issues come up)

You're welcome.
Glad you could finally make it work.