# Painless script outputs literal "key" field

**URL:** https://discuss.elastic.co/t/painless-script-outputs-literal-key-field/373800
**Category:** Elasticsearch
**Tags:** painless, ingest-pipeline
**Created:** [January 29, 2025, 8:03am UTC](https://discuss.elastic.co/t/painless-script-outputs-literal-key-field/373800 "2025-01-29T08:03:47Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Henning\_Oden](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henning_oden/32/123164_2.png) [@Henning\_Oden](https://discuss.elastic.co/u/Henning_Oden)
#### Post date: [January 29, 2025, 8:03am UTC](https://discuss.elastic.co/t/painless-script-outputs-literal-key-field/373800/1 "2025-01-29T08:03:48Z")

</div>

I have a Painless script I run in a Script processor in an Ingest Pipeline which moves fields from within a temporary jsonPayload field to the document root. The source looks like this (field names redacted for safety):

```auto
List bad_fields = new ArrayList();

bad_fields.add("foo");
bad_fields.add("bar");

for (String key : ctx.tmp.jsonPayload.keySet()) {
    if (!bad_fields.contains(key)) {
        ctx.key = ctx.tmp.jsonPayload.get(key)
    }
}

```

The unwanted fields are not moved to the document root, which is all fine and well, and expected. However, it also looks like the value of the alphabetically last field in jsonPayload is put at the field literally named "key" at the document root, which is not expected. Apart from removing the field if it exists, what can I do to prevent this?
