Parsing String as json

1,389 views
Skip to first unread message

Henrik Sjöström

unread,
Feb 29, 2016, 7:45:57 AM2/29/16
to Gatling User Group
I am testing an api that returns json in the form of a string, for example {"stringJson":"{\"stuff\":[\"thing\",\"other thing\"]}"}

Is there a good way to parse this so I can save "other thing"?

My current method is to use jsonPath to save stringJson and then use scala.util.parsing.json.JSON to parse the rest, but that feels rather suboptimal even if it works.

Stéphane LANDELLE

unread,
Feb 29, 2016, 8:49:20 AM2/29/16
to gat...@googlegroups.com

Stéphane Landelle
GatlingCorp CEO


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Henrik Sjöström

unread,
Feb 29, 2016, 10:22:34 AM2/29/16
to Gatling User Group
Since I have a string that needs to be converted to json nested inside the json object could you exemplify with this object how to access "other thing"?

{
"stringJson": "{\"stuff\":[\"thing\",\"other thing\"]}"
}

If it was only a json it would be easy then I could write 

jsonPath("$.stuff[1]")

but I havent managed to get that to work since the value connected to stringJson is a string rather than a json object.

Stéphane LANDELLE

unread,
Feb 29, 2016, 10:33:00 AM2/29/16
to gat...@googlegroups.com
Sorry, I missed that you have escaped JSON inside JSON...

Skip the saving in Session then parse phase: use jsonPath to grab the JSON unescaped String, then parse directly in the check inside a transform.
Don't use scala.util.parsing.json, that's a toy and a perf killer. Use a proper JSON parser. Gatling ships with Boon and Jackson.

Stéphane Landelle
GatlingCorp CEO


--

Henrik Sjöström

unread,
Feb 29, 2016, 11:13:19 AM2/29/16
to gat...@googlegroups.com
Thats significantly nicer.

Thanks
Message has been deleted

Andy Frey

unread,
Jun 22, 2017, 12:32:00 PM6/22/17
to Gatling User Group
I seem to have this exact same issue where I need to path through escaped JSON inside JSON. 

Skip the saving in Session then parse phase: use jsonPath to grab the JSON unescaped String, then parse directly in the check inside a transform.

I'm new to Gatling, so what would this solution look like?

(sorry for the zombie thread)
Reply all
Reply to author
Forward
0 new messages