# Handling optional JSON null value

**URL:** <https://community.gatling.io/t/handling-optional-json-null-value/4668>\
**Category:** Gatling (Open-Source)\
**Created:** [November 23, 2018, 3:55pm UTC](https://community.gatling.io/t/handling-optional-json-null-value/4668 "2018-11-23T15:55:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vu.nguyen](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@vu.nguyen](https://community.gatling.io/u/vu.nguyen)\
**Post date:** [November 23, 2018, 3:55pm UTC](https://community.gatling.io/t/handling-optional-json-null-value/4668/1 "2018-11-23T15:55:44Z")

</div>

Hi Stephane et team,

One of response bodies in my load test can include JSON null value. The value can be seen as following when I have logback trace enabled:

`

body=  
{“id”:1,“token”:null,“time”:12345}

`

The token in this case can either be null or include some text value. I have following check to store its text value when it is available:

`  
.check(jsonPath("""$.token""").find.optional.saveAs(“token”))

`

This check has always worked for me with Gatling 2.X versions. Using Gatling 3.0.1 (latest + missing logback jars), I encountered following Gatling error/failure:

`  
22:31:04.693 [ERROR] i.g.c.a.b.SessionHookBuilder$$anon$1 - ‘hook-128’ crashed with ‘j.l.ClassCastException: Value is null’, forwarding to the next one

`

My understanding is 2 new validations “isNull” and “notNull” are introduced in Gatling 3.0 ([https://gatling.io/docs/current/http/http\_check/#validating](https://gatling.io/docs/current/http/http_check/#validating)). However, I need the “optional” to handle both cases.

Thank you!

Vu

---

<div class="post-metadata">

**Author:** ![vu.nguyen](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@vu.nguyen](https://community.gatling.io/u/vu.nguyen)\
**Post date:** [November 28, 2018, 10:39pm UTC](https://community.gatling.io/t/handling-optional-json-null-value/4668/2 "2018-11-28T22:39:30Z")

</div>

I’d like to add my experiences with the check below:

`  
.check(jsonPath("""$.token""").find.optional.saveAs(“token”))

`

- Gatling 2.2.0: the check does not store “token” in the session when $.token is null
- Gatling 2.3.1: the check stores “token” in the session with null value when $.token is null
- Gatling 3.0.1: the check fails with ClassCastException when $.token is null

Stephane, I think Gatling 2.2.0 has the most desired result in this case.

Thank you for your help.

Vu

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [November 30, 2018, 3:14am UTC](https://community.gatling.io/t/handling-optional-json-null-value/4668/3 "2018-11-30T03:14:27Z")

</div>

> - Gatling 3.0.1: the check fails with ClassCastException when $.token is null

Are you really sure you get the CCE in the check. I suspect you actually get it when try to use the stored value.

The change was discussed in the bug tracker and was about telling the difference between:  
{“id”:1,“token”:null,“time”:12345} \<= value is null  
and  
{“id”:1, “time”:12345} \<= key is undefined

“optional” won’t do any good in your case as “token” is defined and the check is extracting null.

---

<div class="post-metadata">

**Author:** ![vu.nguyen](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@vu.nguyen](https://community.gatling.io/u/vu.nguyen)\
**Post date:** [November 30, 2018, 8:40am UTC](https://community.gatling.io/t/handling-optional-json-null-value/4668/4 "2018-11-30T08:40:13Z")

</div>

I stand corrected. Your suspicion is spot-on, Stephane.

After additional debugging, I found that Gatling 2.3.1 and Gatling 3.0.1 both store “token” in the session when its key is defined and its value is null.

The difference in behavior is when I use the session(“token”) afterward as follows:

`  
val newToken = Option(session(“token”).as[String]).getOrElse(“default”)

`

- With Gatling 2.3.1, newToken is successfully assigned the “default” value.
- With Gatling 3.0.1, ClassCastException is caught. This CCE can be avoided when I use matching with Validation (session(“token”).validate[String]).

Looks like Gatling 3.0.1 behavior in this case is expected.

Thank you again for your help.

Vu
