# Created scripted fields to break out JSON values

**URL:** <https://discuss.elastic.co/t/created-scripted-fields-to-break-out-json-values/175827>\
**Category:** Kibana\
**Created:** [April 8, 2019, 12:14pm UTC](https://discuss.elastic.co/t/created-scripted-fields-to-break-out-json-values/175827 "2019-04-08T12:14:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elastimatic](https://avatars.discourse-cdn.com/v4/letter/e/858c86/32.png) [@elastimatic](https://discuss.elastic.co/u/elastimatic)\
**Post date:** [April 8, 2019, 12:14pm UTC](https://discuss.elastic.co/t/created-scripted-fields-to-break-out-json-values/175827/1 "2019-04-08T12:14:57Z")

</div>

I have been failing repeatedly trying to create a Scripted Field that will break out nested JSON values as separate columns. (They are fields that indicate whether web traffic comes from a bot or spider) What am I missing?

The message looks like:

> message:  
> {  
> "spiderOrRobot": true,  
> "category": "ACTIVE\_SPIDER\_OR\_ROBOT",  
> "reason": "FAILED\_UA\_EXCLUDE",  
> "primaryImpact": "PAGE\_IMPRESSIONS"  
> }

My code to true and return the first JSON object was:

> ```
> > if (doc['message'].value != null) AND (doc['message.spiderOrRobot'].value != null) 
> > return doc['message.spiderOrRobot'].value; 
> > return '';
> 
> ```

This column is always present, but is often NULL. I've tried writing in exceptions to handle it, but I keep getting failed shards.

I tried to follow this post's lead, but no luck yet: [Scripted field conditionals failing (5 of 15 shards failed) - #3 by caub](https://discuss.elastic.co/t/scripted-field-conditionals-failing-5-of-15-shards-failed/147807/3)

---

<div class="post-metadata">

**Author:** ![caub](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/caub/32/33783_2.png) [@caub](https://discuss.elastic.co/u/caub)\
**Post date:** [April 8, 2019, 12:38pm UTC](https://discuss.elastic.co/t/created-scripted-fields-to-break-out-json-values/175827/2 "2019-04-08T12:38:00Z")

</div>

maybe try replacing AND by &&

---

<div class="post-metadata">

**Author:** ![elastimatic](https://avatars.discourse-cdn.com/v4/letter/e/858c86/32.png) [@elastimatic](https://discuss.elastic.co/u/elastimatic)\
**Post date:** [April 8, 2019, 1:14pm UTC](https://discuss.elastic.co/t/created-scripted-fields-to-break-out-json-values/175827/3 "2019-04-08T13:14:45Z")

</div>

haha, sorry, that was a forehead slapper - been switching between SQL too much this morning. Rewrote it multple ways. I sitll can't figure out why it isn't working.

Current iteration is:

> if (!doc['message'].empty && !doc['message.spiderOrRobot'].empty) {  
> return doc['message.spiderOrRobot'].value;  
> }
> 
> return '';

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 6, 2019, 1:14pm UTC](https://discuss.elastic.co/t/created-scripted-fields-to-break-out-json-values/175827/4 "2019-05-06T13:14:58Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
