Problems with json response and several instances of an object. Correlating.

I have a request that returns a json body which I only want to capture one of two objects. I want to count the number of occurrences for the membershipId:32041428 below, not number of occurrences for the second mebershipId: 8315319241

earlier this worked fine (


.check(jsonPath("$.membershipCoupons[*].coupons[?(@.activationDate==null)].offerId").findAll.optional.saveAs("itemList"))

but now the responses could contain more than one membership, and I only want to capture the number of offerids for the mebershipId I parameterize with my .csv with column ${medlem}

`

{
“resultCode”: “SUCCESS”,
“errorCode”: null,
“errorMessage”: null,
“membershipCoupons”:
[
{
“membershipId”: “32041428”,
“coupons”:
[
{
“offerId”: “000C291EE9241ED4AAF45058D8564910”,
“description”: “merchant1”,
“graphics”:
[
{
“width”: 400,
“height”: 200,
“quality”: “Low”,
“url”: “https://site/02_201506_011/000C291EE8241ED4AAF43058D8564910Low.jpg
},
{
“width”: 900,
“height”: 600,
“quality”: “High”,
“url”: “https://site/02_201506_011/000C291EE8241ED4AAF43058D8564910High.jpg
}
],
“endTime”: 1423353600000,
“activationDate”: 1423579699407,
“redemption”: null
},
]
},
{
“membershipId”: “8315319241”,
“coupons”:
[
{
“offerId”: “000C291EE8241ED4AAF43058D85CA910”,
“description”: “merchant2”,
“graphics”:
[
{
“width”: 400,
“height”: 200,
“quality”: “Low”,
“url”: “https://site/02_201506_011/000C291EE8241ED4AAF43058D85CA910Low.jpg
},
{
“width”: 900,
“height”: 600,
“quality”: “High”,
“url”: “https://site/02_201506_011/000C291EE8241ED4AAF43058D85CA910High.jpg
}
]
}
]
}

`

I tried to put the parameter for the ‘in-session’ member like this:

I did use http://jsonpath.curiousconcept.com
and I made it work there with this:

`
$.membershipCoupons[?(@.membershipId==8315530485)].coupons[?(@.activationDate==null)].offerId

`

But if I hard-code this into my Simulation it does not work…

Strange!

Now I am very close. I have set the member ‘in session’ and I managed to print it.

Now I just want to do a jsonPath .check (and saveAs) with the condition of the members in session.

`

val scn = scenario("Scenario")
    .feed(csv("data/medlemmer.csv").random)
    .exec(_.set("itemList", Seq.empty[Int]))

    //get token
    .exec(
      http("getToken")
        .post......
             ......
              .....
        .check(jsonPath("$.access_token").exists.saveAs("token")))

    .exec(
      http("getProfile")
        .get("/user/profile")
        .header( "X-Token", "${token}" )
        .check(status.is(200))
        .check(jsonPath("$.resultCode").is("SUCCESS"))
    )

  .exec(session => {
    val str = session("medlem").as[String]
    session.set("str", str)
  })

  .exec(session => {
    println((session("str").as[String]))
    session})

    .exec(
      http("getCouponList")
        .get("/coupon/all")
        .header("X-Token", "${token}")
        .check(status.is(200))
        .check(jsonPath("$.membershipCoupons[?(@.membershipId==**"str"**})].coupons[?(@.activationDate==null)].offerId").findAll.optional.saveAs("itemList"))

     )

    .doIf(session => ! session("itemList").as[Seq[String]].isEmpty)
  {
  foreach("${itemList}", "item")
  {
    exec(session => {
    println((session("item").as[String]))
    session})
  }
}

`

The line I need help to find out is this:

`

.check(jsonPath("$.membershipCoupons[?(@.membershipId==**"str"**})].coupons[?(@.activationDate==null)].offerId").findAll.optional.saveAs("itemList"))

`

How do I include the “str” into the jsonPath check?

membershipId is a String in your JSON payload, nor a number.

Now I am down to this working:

`

.check(jsonPath("$.membershipCoupons[0].coupons[?(@.activationDate==null)].offerId").findAll.optional.saveAs("itemList"))

`

or

`

.check(jsonPath("$.membershipCoupons[1].coupons[?(@.activationDate==null)].offerId").findAll.optional.saveAs("itemList"))

`

gives me the offers for the two members in the json response.

But doing this does not work:

`

.check(jsonPath("$.membershipCoupons[?(@.membershipId==58030643)].coupons[?(@.activationDate==null)].offerId").findAll.optional.saveAs("itemList"))

`

as in hardcoding it.

The json response is:

`

{
“resultCode”: “SUCCESS”,
“errorCode”: null,
“errorMessage”: null,
“membershipCoupons”:
[
{
“membershipId”: “183346852”,
“coupons”:
[
{
“offerId”: “000C291EE8241ED4AAF43058D851C910”,
“description”: “marchant1”,
“graphics”:
[
{
“width”: 400,
“height”: 200,
“quality”: “Low”,
“url”: “https://site/02_201506_011/000C291EE8241ED4AAF43058D851C910Low.jpg
},
{
“width”: 900,
“height”: 600,
“quality”: “High”,
“url”: “https://site/02_201506_011/000C291EE8241ED4AAF43058D851C910High.jpg
}
],
“endTime”: 1423353600000,
“activationDate”: null,
“redemption”: null
}
]
}
{
“membershipId”: “8315320325”,
“coupons”:
[
{
“offerId”: “000C291EE8241ED4AAF43058D855A910”,
“description”: “marchant2”,
“graphics”:
[
{
“width”: 400,
“height”: 200,
“quality”: “Low”,
“url”: “https://site/02_201506_011/000C291EE8241ED4AAF43058D855A910Low.jpg
},
{
“width”: 900,
“height”: 600,
“quality”: “High”,
“url”: “https://site/02_201506_011/000C291EE8241ED4AAF43058D855A910High.jpg
}
],
“endTime”: 1423353600000,
“activationDate”: null,
“redemption”: null
}
]
}
]
}

`

I want to get there where i can say:

`

.check(jsonPath("$.membershipCoupons[?(@.membershipId=='str')].coupons[?(@.activationDate==null)].offerId").findAll.optional.saveAs("itemList"))

`

Ans gatling uses the ‘str’ in session to do the check and save the offerid’s for that (‘str’) memberId.

Yes, but I do make it to a string in my session like this:

`

.exec(session => {
  val str = session("medlem").as[String]
  session.set("str", str)
})

`

before I do the check.

Point by point I do:

`

.feed(csv("data/medlemmer.csv").random)

`

then I pick a value from ‘medlemmer.csv’:

`

.exec(session => {
  val str = session("medlem").as[String]
  session.set("str", str)
})

`

I can print the value here with:

`

.exec(session => {
  println((session("str").as[String]))
  session})

`

The I do the getList() and then I try to fetch offereId’s for the memberId in ‘str’

like this:

`

.exec(
  http("getCouponList")
    .get("/coupon/all")
    .header("X-Token", "${token}")
    .check(status.is(200))

    .check(jsonPath("$.membershipCoupons[?(@.membershipId=="toString("str"))].coupons[?(@.activationDate==null)].offerId").findAll.optional.saveAs("itemList"))
 )

`

But I am not sure how to state this in scala…

the part:

`

[?(@.membershipId=="toString("str"))]

`

I just inform that this works using ‘${MemberId}’

`

.check(jsonPath("$.membershipCoupons[?(@.membershipId=='${MemberId}')].coupons[?(@.activationDate==null)].offerId").findAll.optional.saveAs("itemList"))

`