# Pipeline containsKey check fails when top-level object does not exist

**URL:** <https://discuss.elastic.co/t/pipeline-containskey-check-fails-when-top-level-object-does-not-exist/310130>\
**Category:** Elasticsearch\
**Tags:** ingest-pipeline\
**Created:** [July 20, 2022, 10:19am UTC](https://discuss.elastic.co/t/pipeline-containskey-check-fails-when-top-level-object-does-not-exist/310130 "2022-07-20T10:19:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SecretSquizza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/secretsquizza/32/77449_2.png) [@SecretSquizza](https://discuss.elastic.co/u/SecretSquizza)\
**Post date:** [July 20, 2022, 10:19am UTC](https://discuss.elastic.co/t/pipeline-containskey-check-fails-when-top-level-object-does-not-exist/310130/1 "2022-07-20T10:19:25Z")

</div>

Hi everyone!

I'm struggling to find an answer to this issue and i keep receiving the error

> Cannot invoke "Object.getClass()" because "value" is null

The problem occurs when I have an object - in this case **error** - that may or may not exist but I need to check for the existence of a sub-field within that object.

`"if" : "ctx.error?.containsKey('code')"`

This works when the **error** object exists but the field **code** does not exist, but fails when the entire **error** object is absent, with the above error.

Anyone know how to resolve?

Many thanks in advance.

---

<div class="post-metadata">

**Author:** ![SecretSquizza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/secretsquizza/32/77449_2.png) [@SecretSquizza](https://discuss.elastic.co/u/SecretSquizza)\
**Post date:** [July 20, 2022, 12:34pm UTC](https://discuss.elastic.co/t/pipeline-containskey-check-fails-when-top-level-object-does-not-exist/310130/2 "2022-07-20T12:34:58Z")

</div>

I found the answer i needed - i'll post here in case anyone else struggles with a similar problem.

The resolution is to check the existence of the top-level object and then the existence of the field within the object in the same conditional if statement. It seems that the logic short-circuits as soon as the condition is false so doesn't go on to check the existence of the field if the object check fails:

`"if" : "ctx.containsKey('error') && ctx['error'].containsKey('code')"`

---

<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:** [August 17, 2022, 12:35pm UTC](https://discuss.elastic.co/t/pipeline-containskey-check-fails-when-top-level-object-does-not-exist/310130/3 "2022-08-17T12:35:47Z")

</div>

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