extracting from response based on criteria in JSON response structure

My JSON response:

`

{
   "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’ ?

.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’

Try something like this:

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

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

I am running the path expression in Stefan Gossner 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?

if there are more than one offerIds matching the criteria

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

)}

`

and process them in a loop

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

??

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”

"activationDate": 04112014,
"redemption": 04112014

Obviously, those should be quoted strings, not numbers.

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):

`

{"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/) 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

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

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

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”)
.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:

`

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

You are correct. Thanks for all the great help