Need help : Json and session.getV

Hello

I can’t save and use JSON data.
That’s my sample :

val login = scenario(“login”)
.exec(http(“login”)
.post(“https://XX/login”)
.body(""“email=XX&password=XX”"")
.check(jsonPath("$.response").find.saveAs(“save”))
)
.exec(session => {
val test = session.getVJSONArray
println(test->(“errorCode”))
session
})

(Failure(Can’t cast value (xxxxxxxxxxxxxxxxxx the body response xxxxxxxxxxxxxxxxxx ) of type class java.String into class scala.util.parsing.json.JSONArray), errorCode)

So i can’t cast “save” into JSON format ?:frowning:
Or is there a function to transform String into Json ?
The jsonPath works well, but i’m not able to reuse my save. ( saveAsJSON ? )
I want to use JSON, because the response is in this format and i will have to parse it and do some stuff with it.

Thanks
Théo

JsonPath currently extracts Strings. The reason is that we haven’t find yet how to have it return objects and match them in the matching part of the check.
Still investigating.

Currently, if you want JSON, you’ll have to parse the String with the engine of your choice (Gatling ships json-smart).

As you only seem to be interested in performing a save and not verifying, you can use a bodyString check with a transform step that would parse the response body String and extract the part of the JSON tree you’re interested in.

https://github.com/excilys/gatling/wiki/Checks#wiki-bodyString

https://github.com/excilys/gatling/wiki/Checks#wiki-transforming

I found that to use json-smart:
/*

.check(bodyString.transform(_.map(string => {

val json = parser.parse(is).asInstanceOf[JSONObject]

json.get(“data”).asInstanceOf[JSONObject].collect {

case (key, keyValue: JSONObject) if keyValue.get(“foo”) == true => key

}

})).saveAs(“foos”)))

*/
But it doesn’t work and i don’t understand :smiley:

I can do that:
.check(jsonPath("$.response.errorMessage").saveAs(errorMessage))
.check(jsonPath("$.response.errorCode").saveAs(errorCode))
.check(jsonPath("$.response.information.Id").saveAs(Id))


but… :frowning:

And i have some troubles with the get. If i use “session.get” it returns the information and adds “Some(” at the begin. If i use “session.getV[String]” it adds “Success(”…
should i use something to remove that ?

Thanks

In the version you use, session.get returns an Option, not the value.

Could you first upgrade to the timestamp 2.0.0.20130605 I deployed a few days ago, please?We’ve changed the Session syntax https://github.com/excilys/gatling/issues/1175#issuecomment-18208429 and it would be easier for me to assist you with the new one.

Hi ,

I have used
I found that to use json-smart:
/*

.check(bodyString.transform(_.map(string => {

val json = parser.parse(is).asInstanceOf[JSONObject]

json.get(“data”).asInstanceOf[JSONObject].collect {

case (key, keyValue: JSONObject) if keyValue.get(“foo”) == true => key

}

})).saveAs(“foos”)))

*/

But its giving me following error bject parse is not a member of package net.minidev.json.parser.

Regards