# Extracting JSON response

**URL:** <https://community.gatling.io/t/extracting-json-response/1418>\
**Category:** Gatling (Open-Source)\
**Created:** [September 15, 2014, 12:16pm UTC](https://community.gatling.io/t/extracting-json-response/1418 "2014-09-15T12:16:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [September 15, 2014, 12:16pm UTC](https://community.gatling.io/t/extracting-json-response/1418/1 "2014-09-15T12:16:23Z")

</div>

Using Firefox RESTClient gives me the response(body) attached as screenshot.  
Wanting to save i.e the “scanNumber” in this response, how do I write that in Gatling?  
I have tried

.check(jsonPath("//“scanNumber”:"").saveAs(“responsevar”)))

But get error:

14:14:36.019 [WARN] i.g.h.a.AsyncHandlerActor - Request ‘Login page’ failed: jsonPath(“scanNumber”:"").exists failed, could not extract: could not extract : `$' expected but `’ found

Is there a way to print to console (in i.e IntelliJ) the reponse I am getting in the Simulation? This would be helpful.

Given that I have managed to save the response “responsevar” above is there an easy way to also print this to console in i.e IntelliJ?

Magnus

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [September 15, 2014, 12:33pm UTC](https://community.gatling.io/t/extracting-json-response/1418/2 "2014-09-15T12:33:02Z")

</div>

And here is the screenshot.

Magnus

 ![Sp user group extracting.jpg](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/1X/dda4551149707c3764020368fa771e71403d89f1.jpeg)

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [September 15, 2014, 12:36pm UTC](https://community.gatling.io/t/extracting-json-response/1418/3 "2014-09-15T12:36:37Z")

</div>

Click on link on documentation and check the proper syntax.  
JsonPath ≠ XPath

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [September 15, 2014, 7:03pm UTC](https://community.gatling.io/t/extracting-json-response/1418/4 "2014-09-15T19:03:14Z")

</div>

Looking at [http://gatling.io/docs/2.0.0-RC5/http/http\_check.html](http://gatling.io/docs/2.0.0-RC5/http/http_check.html)  
this is still not clear to me.

Could you please elaborate? JSON is different from XPath, ok. But I miss a concrete example in the documentation.

Thanks,  
Magnus

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [September 15, 2014, 7:09pm UTC](https://community.gatling.io/t/extracting-json-response/1418/5 "2014-09-15T19:09:02Z")

</div>

[https://github.com/gatling/gatling/blob/master/gatling-core/src/test/scala/io/gatling/core/check/extractor/jsonpath/JsonPathExtractorSpec.scala](https://github.com/gatling/gatling/blob/master/gatling-core/src/test/scala/io/gatling/core/check/extractor/jsonpath/JsonPathExtractorSpec.scala)

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [September 15, 2014, 7:20pm UTC](https://community.gatling.io/t/extracting-json-response/1418/6 "2014-09-15T19:20:57Z")

</div>

Right,  
I did make this work:

.check(jsonPath(“$.resultCode”).is(“SUCCESS”)))

But this:

.check(jsonPath(“$.memberships.scanNumber”).saveAs(“scanNr”)))

returns:

> jsonPath($.memberships.scanNumber).exists, found nothing 5 (100,0%)

I guess I am getting closer, do I need the .check ? It should find “something” looking at the JSON response again:

```auto
{"resultCode":"SUCCESS","errorCode":null,"errorMessage":null,"profile":{"fullName":"TestFirstName TestMiddleName TestLastName","memberships":[{"name":"UA Gold Partner","number":"123-456-123-123","scanNumber":"123-456-123-123"}]}}

```

---

<div class="post-metadata">

**Author:** ![Pierre\_DAL-PRA](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@Pierre\_DAL-PRA](https://community.gatling.io/u/Pierre_DAL-PRA)\
**Post date:** [September 15, 2014, 7:23pm UTC](https://community.gatling.io/t/extracting-json-response/1418/7 "2014-09-15T19:23:24Z")

</div>

_memberships_ is an array : _$.memberships[0].scanNumber_

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [September 15, 2014, 7:31pm UTC](https://community.gatling.io/t/extracting-json-response/1418/8 "2014-09-15T19:31:10Z")

</div>

Still not working with:

.check(jsonPath("$.memberships[0].scanNumber").findAll.saveAs(“scanNr”)))

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [September 15, 2014, 7:34pm UTC](https://community.gatling.io/t/extracting-json-response/1418/9 "2014-09-15T19:34:24Z")

</div>

But this worked:

.check(jsonPath("$.profile.memberships[0].scanNumber").findAll.saveAs(“scanNr”)))

Thank you guys!

---

<div class="post-metadata">

**Author:** ![Pierre\_DAL-PRA](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@Pierre\_DAL-PRA](https://community.gatling.io/u/Pierre_DAL-PRA)\
**Post date:** [September 15, 2014, 7:34pm UTC](https://community.gatling.io/t/extracting-json-response/1418/10 "2014-09-15T19:34:50Z")

</div>

Woops : _$.profile.memberships[0].scanNumber_

For further info on JsonPath: [http://goessner.net/articles/JsonPath/](http://goessner.net/articles/JsonPath/) (link is provided in the documentation).  
You can also easily find online JSONpath expression testers.

---

<div class="post-metadata">

**Author:** ![fugmag](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@fugmag](https://community.gatling.io/u/fugmag)\
**Post date:** [September 15, 2014, 7:37pm UTC](https://community.gatling.io/t/extracting-json-response/1418/11 "2014-09-15T19:37:04Z")

</div>

Thanks,  
is it a simple way to print the value of scanNumber saved as the variable “scanNr” in my question.  
print to console in IntelliJ i.e.?

Cheers,  
Magnus

---

<div class="post-metadata">

**Author:** ![Pierre\_DAL-PRA](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@Pierre\_DAL-PRA](https://community.gatling.io/u/Pierre_DAL-PRA)\
**Post date:** [September 15, 2014, 7:39pm UTC](https://community.gatling.io/t/extracting-json-response/1418/12 "2014-09-15T19:39:23Z")

</div>

Add a session lambda to your scenario to print your value stored in the session, after it’s set by your check:

_exec(session =\> {_  
_println(session(“scanNr”).as[String])_  
_session_  
_})_
