# Problem overwriting field using json filter plugin

**URL:** <https://discuss.elastic.co/t/problem-overwriting-field-using-json-filter-plugin/299132>\
**Category:** Logstash\
**Created:** [March 8, 2022, 6:35pm UTC](https://discuss.elastic.co/t/problem-overwriting-field-using-json-filter-plugin/299132 "2022-03-08T18:35:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tstrul](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@tstrul](https://discuss.elastic.co/u/tstrul)\
**Post date:** [March 8, 2022, 6:35pm UTC](https://discuss.elastic.co/t/problem-overwriting-field-using-json-filter-plugin/299132/1 "2022-03-08T18:35:48Z")

</div>

Hey,  
I have a log that i want to parse as json. field is "log"  
I want to decode json but use the same field as the orig message.  
when i try:

```auto
      json {
        source => "log"
        target => "log"
      }

```

Im getting this error:

```auto
[2022-03-08T18:24:57,514][WARN][logstash.outputs.elasticsearch][main] Could not index event to Elasticsearch. {:status=>400, :action=>["index", {:_id=>nil, :_index=>"XXXX-2022.03.08", :_type=>"_doc", :routing=>nil}, #<LogStash::Event:0x41fe54e5>], :response=>{"index"=>{"_index"=>"XXXX-2022.03.08", "_type"=>"_doc", "_id"=>"Uz3Gan8Bm8PSZeTG8hCn", "status"=>400, "error"=>{"type"=>"mapper_parsing_exception", "reason"=>"failed to parse field [log] of type [text] in document with id 'Uz3Gan8Bm8PSZeTG8hCn'. "caused_by"=>{"type"=>"illegal_state_exception", "reason"=>"Can't get text on a START_OBJECT at 1:54"}}}}}

```

If i map target to a new field, everything works.  
any idea why that happens?  
the documentation mentions that:  
" if the `target` field already exists, it will be overwritten!"

Thank you!

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 8, 2022, 7:39pm UTC](https://discuss.elastic.co/t/problem-overwriting-field-using-json-filter-plugin/299132/2 "2022-03-08T19:39:58Z")

</div>

> [@tstrul](#):
>
> any idea why that happens?

Read [this](https://discuss.elastic.co/t/getting-illegal-state-exception-error-while-pushing-logs-to-elasticsearch/290029/2) thread. You have indexed documents where the [log] field is a string, and now you are trying to index documents where [log] is an object in the same index. That does not work.

---

<div class="post-metadata">

**Author:** ![tstrul](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@tstrul](https://discuss.elastic.co/u/tstrul)\
**Post date:** [March 8, 2022, 8:27pm UTC](https://discuss.elastic.co/t/problem-overwriting-field-using-json-filter-plugin/299132/3 "2022-03-08T20:27:29Z")

</div>

Thank you very much that was very helpful!  
I understand the problem but im not sure i understand the solution though...

Currently, "log" field mostly contains json but from time to time it gets regular strings from legacy components.

index template defines this as a string.  
I can change that but according to the thread you mentioned that will cause trouble as well.

I know that json filter can handle parsing errors and leave data untouched (\_jsonparsefailure) but how should i define index template for this field ?

so is there another option besides changing the target field for json?

Thank you

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 8, 2022, 9:17pm UTC](https://discuss.elastic.co/t/problem-overwriting-field-using-json-filter-plugin/299132/4 "2022-03-08T21:17:10Z")

</div>

> [@tstrul](#):
>
> Currently, "log" field mostly contains json but from time to time it gets regular strings from legacy components.

It cannot be an object on some events and a string on others. Pick one or the other and set it with the template (I think you would set the mapping to `"type": "object"`, but that is an elasticsearch question and I do not run elasticsearch).

If you want to convert the strings to objects you would use

```
ruby {
     code => '
        host = event.get("log")
        if ! host.is_a? Hash
            event.set("[log][log]", event.remove("log"))
        end
    '
}

```

---

<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:** [April 5, 2022, 9:17pm UTC](https://discuss.elastic.co/t/problem-overwriting-field-using-json-filter-plugin/299132/5 "2022-04-05T21:17:20Z")

</div>

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