# extracting from response based on criteria in JSON response structure

**URL:** <https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648>\
**Category:** Gatling (Open-Source)\
**Created:** [November 5, 2014, 9:59pm UTC](https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648 "2014-11-05T21:59:59Z")\
**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:** [November 5, 2014, 9:59pm UTC](https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648/1 "2014-11-05T21:59:59Z")

</div>

My JSON response:

`

```auto
{
   "resultCode": "SUCCESS",
   "errorCode": null,
   "errorMessage": null,
   "membershipCoupons":
   [
       {
           "membershipId": "3459",
           "coupons":
           [
               {
                   "offerId": "000",
                   "description": "FELIX",
                   "graphics":
                   [
                       {
                           "width": 400,
                           "height": 200,
                           "quality": "Low",
                           "url": " http://www.someurl.com:8080/297Low.jpg"
                       },
                       {
                           "width": 900,
                           "height": 600,
                           "quality": "Hight",
                           "url": " http://www.someurl.com:8080/65F7High.jpg"
                       }
                   ],
                   "endTime": 1417305600000,
                   "activationDate": null,
                   "redemption": null
               },
               {
                   "offerId": "000C29785F7",
                   "description": "this_thing_3 ",
                   "graphics":
                   [
                       {
                           "width": 400,
                           "height": 200,
                           "quality": "Low",
                           "url": " http://www.someurl.com:8080/D085F7Low.jpg"
                       },
                       {
                           "width": 900,
                           "height": 600,
                           "quality": "Hight",
                           "url": " http://www.someurl.com:8080/85F7High.jpg"
                       }
                   ],
                   "endTime": 1417305600000,
                   "activationDate": 01112014,
                   "redemption": 01112014
               },
               {
                   "offerId": "000CF7",
                   "description": "Thing 7,- ",
                   "graphics":
                   [
                       {
                           "width": 400,
                           "height": 200,
                           "quality": "Low",
                           "url": " http://www.someurl.com:8080/0F7Low.jpg"
                       },
                       {
                           "width": 900,
                           "height": 600,
                           "quality": "Hight",
                           "url": " http://www.someurl.com:8080/7High.jpg"
                       }
                   ],
                   "endTime": 1417305600000,
                   "activationDate": 04112014,
                   "redemption": 04112014
               }
           ]
       }
   ]
}

```

`

I need to save only the offerId where ‘activationDate=null’

Could this be done in an expression like this (tweaked to only save where ‘activationDate=null’ ?

```auto
.check(jsonPath("$.membershipCoupons[0].coupons[0].offerId").saveAs("offerId")))

```

`

`

or do I need to implement some scala code to extract only the ‘node’ with ‘activationDate=null’

---

<div class="post-metadata">

**Author:** ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)\
**Post date:** [November 6, 2014, 12:45am UTC](https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648/2 "2014-11-06T00:45:31Z")

</div>

Try something like this:

$.membershipCoupons[\*].coupons[?(@.activationDate==null)].offerId

---

<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:** [November 6, 2014, 9:22am UTC](https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648/3 "2014-11-06T09:22:25Z")

</div>

Ok, thank you, but what if there are more coupons with value activationDate=null ? would it pick up all of them with this expression u think?

And the top level of the ‘nodes’ I want to pick up the ID’s from are offerId’s not coupons, shouldn’t it be offerid’s that is matched for @activationDate==null?

Cheers

---

<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:** [November 6, 2014, 10:37am UTC](https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648/4 "2014-11-06T10:37:38Z")

</div>

I am running the path expression in Stefan Gossner [http://jsonpath.curiousconcept.com/](http://jsonpath.curiousconcept.com/) and get vale ‘false’, but the expression: membershipCoupons[0].coupons[0].offerId"  
works

Could there be some syntax error int the expression: membershipCoupons[\*].coupons[?(@.activationDate==null)].offerId

and is it a problem that there are more instances of offerId’s?

---

<div class="post-metadata">

**Author:** ![Neil](https://avatars.discourse-cdn.com/v4/letter/n/839c29/32.png) [@Neil](https://community.gatling.io/u/Neil)\
**Post date:** [November 6, 2014, 11:34am UTC](https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648/5 "2014-11-06T11:34:10Z")

</div>

if there are more than one offerIds matching the criteria

`  
.foreach("${offerId}", “id”) {  
exec(  
…  
)}

`

and process them in a loop

---

<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:** [November 6, 2014, 1:28pm UTC](https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648/6 "2014-11-06T13:28:16Z")

</div>

But what is wrong with the jsonpath expression?  
Is it some syntax error here?

membershipCoupons[\*].coupons[?(@.activationDate==null)].offerId

given that this works:

membershipCoupons[0].coupons[0].offerId

??

---

<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:** [November 6, 2014, 2:49pm UTC](https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648/7 "2014-11-06T14:49:16Z")

</div>

What I’m sure of is that your JSON is invalid. Even Jackson complains about it.

“com.fasterxml.jackson.core.JsonParseException: Invalid numeric value: Leading zeroes not allowed”

```auto
"activationDate": 04112014,
"redemption": 04112014

```

Obviously, those should be quoted strings, not numbers.

---

<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:** [November 6, 2014, 3:11pm UTC](https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648/8 "2014-11-06T15:11:51Z")

</div>

I did manipulated it a bit to illustrate a point that activationDate also could have values.  
However this is the actual JSON (only manipulated the URL’s):

`

```auto
{"resultCode":"SUCCESS","errorCode":null,"errorMessage":null,"membershipCoupons":[{"membershipId":"8107222059","coupons":[{"offerId":"000C297765971EE495BE0BA921A4C5F7","description":"G ","graphics":[{"width":400,"height":200,"quality":"Low","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921A4C5F7Low.jpg"},{"width":900,"height":600,"quality":"Hight","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921A4C5F7High.jpg"}],"endTime":1417305600000,"activationDate":null,"redemption":null},{"offerId":"000C297765971EE495BE0BA921C065F7","description":"KG ","graphics":[{"width":400,"height":200,"quality":"Low","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921C065F7Low.jpg"},{"width":900,"height":600,"quality":"Hight","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921C065F7High.jpg"}],"endTime":1417305600000,"activationDate":null,"redemption":null},{"offerId":"000C297765971EE495BE0BA921C285F7","description":"5L ","graphics":[{"width":400,"height":200,"quality":"Low","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921C285F7Low.jpg"},{"width":900,"height":600,"quality":"Hight","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921C285F7High.jpg"}],"endTime":1417305600000,"activationDate":null,"redemption":null},{"offerId":"000C297765971EE495BE0BA921C305F7","description":"NOG ","graphics":[{"width":400,"height":200,"quality":"Low","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921C305F7Low.jpg"},{"width":900,"height":600,"quality":"Hight","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921C305F7High.jpg"}],"endTime":1417305600000,"activationDate":null,"redemption":null},{"offerId":"000C297765971EE495BE0BA921CAE5F7","description":"KB ","graphics":[{"width":400,"height":200,"quality":"Low","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921CAE5F7Low.jpg"},{"width":900,"height":600,"quality":"Hight","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921CAE5F7High.jpg"}],"endTime":1417305600000,"activationDate":null,"redemption":null},{"offerId":"000C297765971EE495BE0BA921CE65F7","description":"W0 ","graphics":[{"width":400,"height":200,"quality":"Low","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921CE65F7Low.jpg"},{"width":900,"height":600,"quality":"Hight","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921CE65F7High.jpg"}],"endTime":1417305600000,"activationDate":null,"redemption":null},{"offerId":"000C297765971EE495BE0BA921D085F7","description":"BS ","graphics":[{"width":400,"height":200,"quality":"Low","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921D085F7Low.jpg"},{"width":900,"height":600,"quality":"Hight","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921D085F7High.jpg"}],"endTime":1417305600000,"activationDate":null,"redemption":null},{"offerId":"000C297765971EE495BE0BA921D585F7","description":"B7,- ","graphics":[{"width":400,"height":200,"quality":"Low","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921D585F7Low.jpg"},{"width":900,"height":600,"quality":"Hight","url":"\n http://www.swe.se:8080/02_201442_005/000C297765971EE495BE0BA921D585F7High.jpg"}],"endTime":1417305600000,"activationDate":null,"redemption":null}]}]}

```

`

and no values for activationDate. Pasting it into a json-xpath tool ([http://jsonpath.curiousconcept.com/](http://jsonpath.curiousconcept.com/)) gives me a correct value in return using the xpath:  
.check(jsonPath("$.membershipCoupons[0].coupons[0].offerId").saveAs(“offerId”)))

but gives me ‘false’ using:

.check(jsonPath("$.membershipCoupons[\*].coupons[?@activationDate==null].offerId").saveAs(“offerId”)))

What is the matter with the last jsonpath expression?

Cheers

---

<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:** [November 6, 2014, 3:23pm UTC](https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648/9 "2014-11-06T15:23:04Z")

</div>

I'd say the issue is actually with [http://jsonpath.curiousconcept.com/](http://jsonpath.curiousconcept.com/).

Both [http://ashphy.com/JSONPathOnlineEvaluator](http://ashphy.com/JSONPathOnlineEvaluator) and Gatling give me the  
expected result: "000C297765971EE495BE0BA921A4C5F7".

---

<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:** [November 6, 2014, 3:56pm UTC](https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648/10 "2014-11-06T15:56:44Z")

</div>

When I run my Gatling simulation

`

import io.gatling.core.Predef.\_  
import io.gatling.http.Predef.\_

class AppSimulation extends Simulation {

val httpConf = http  
.baseURL(“[https://someurl.com](https://someurl.com)”)  
.acceptEncodingHeader(“gzip,deflate”)  
.headers(Map(“Content-Type” → “application/json”, “charset” → “UTF-8”, “User-Agent” → “Android(4.4)/Sweet(0.4)/0.1”))

val scn = scenario(“Scenario”)  
.feed(csv(“memberInfo.csv”).random)

.exec(\_.set(“TOKEN”, “B67CE079-170B-4F8E-B261-2668FCBAC974”))

.exec(  
http(“getProfile”)  
.get("/user/profile")  
.header( “X-Token”, “${TOKEN}” )  
.check(status.is(200))  
.check(jsonPath("$.resultCode").is(“SUCCESS”))  
.check(jsonPath("$.profile.memberships[0].scanNumber").saveAs(“scanNr”)))

.exec(session =\> {  
println((session(“scanNr”).as[String]))  
session  
})

.exec(  
http(“getCouponList”)  
.get("/coupon/all")  
.header(“X-Token”, “${TOKEN}”)  
.check(status.is(200))  
.check(jsonPath("$.resultCode").is(“SUCCESS”))  
.check(jsonPath("$.membershipCoupons[\*].coupons[?@activationDate==null].offerId").saveAs(“offerId”)))  
//.check(jsonPath("$.membershipCoupons[0].coupons[0].offerId").saveAs(“offerId”)))

.exec(session2 =\> {  
println((session2(“offerId”).as[String]))  
session2  
})

.exec(  
http(“activateCoupon”)  
.post("/coupon/activate")  
.header(“X-Token”, “${TOKEN}”)  
.body(ELFileBody(“activateCoupons.json”)).asJSON  
.check(status.is(200))  
.check(jsonPath("$.resultCode").is(“SUCCESS”)))

//logout

setUp(scn.inject(constantUsersPerSec(1) during (1))).protocols(httpConf)  
}

`

I get this:

`

---

<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:** [November 6, 2014, 4:21pm UTC](https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648/11 "2014-11-06T16:21:22Z")

</div>

A few post before, your expression was correct: membershipCoupons[\*].coupons[?(@.activationDate==null)].offerId

Now I see that you have $.membershipCoupons[\*].coupons[?@activationDate==null].offerId, which is wrong

---

<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:** [November 6, 2014, 4:34pm UTC](https://community.gatling.io/t/extracting-from-response-based-on-criteria-in-json-response-structure/1648/12 "2014-11-06T16:34:04Z")

</div>

You are correct. Thanks for all the great help
