# Json parse return filled with NULL

**URL:** <https://discuss.elastic.co/t/json-parse-return-filled-with-null/175175>\
**Category:** Logstash\
**Created:** [April 3, 2019, 11:09am UTC](https://discuss.elastic.co/t/json-parse-return-filled-with-null/175175 "2019-04-03T11:09:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ep4sh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ep4sh/32/40099_2.png) [@ep4sh](https://discuss.elastic.co/u/ep4sh)\
**Post date:** [April 3, 2019, 11:09am UTC](https://discuss.elastic.co/t/json-parse-return-filled-with-null/175175/1 "2019-04-03T11:09:33Z")

</div>

I parse JSON, and even If JSON is invalid I want to save it.  
So I created an additional variable " **invalid**". If there is an exception, the method should return "saved" invalid var, but it doesn't -

```
def jsonParse(str, ev)
    invalid = str
begin
    parsed = JSON.parse(str)
rescue => e
    ev.tag("_parseJsonFail")
    return invalid
end
return parsed
end

```

I run code with ruby filter plugin (code option):

```
#...some parse magic.....
#
jsmap.each_pair {|key, value| event.set("#{key}", jsonParse(value, event)) }

```

If i feed pipeline with invalid JSON - fields after _event.set_ filled by "null":

![image](https://us1.discourse-cdn.com/elastic/original/3X/6/5/657cf397234d3c6e59bf4da55dbf625b013c3288.png)

But tagged:

![image](https://us1.discourse-cdn.com/elastic/original/3X/9/1/9134962f8e5698db10141716c0b6b1bee36c4ef3.png)

What I do wrong with return op?

---

<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:** [April 3, 2019, 2:18pm UTC](https://discuss.elastic.co/t/json-parse-return-filled-with-null/175175/2 "2019-04-03T14:18:57Z")

</div>

> [@ep4sh](#):
>
> What I do wrong with return op?

I do not see anything wrong with that. This gets me "a" =\> "foo"

```
        code => '
            def jsonParse(str, ev)
                invalid = str
                begin
                    parsed = JSON.parse(str)
                rescue => e
                    ev.tag("_parseJsonFail")
                    return invalid
                end
                return parsed
            end
            event.set("a", jsonParse("foo", event))
        '

```

What is cc set to in the input?

---

<div class="post-metadata">

**Author:** ![ep4sh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ep4sh/32/40099_2.png) [@ep4sh](https://discuss.elastic.co/u/ep4sh)\
**Post date:** [April 3, 2019, 2:21pm UTC](https://discuss.elastic.co/t/json-parse-return-filled-with-null/175175/3 "2019-04-03T14:21:10Z")

</div>

Yep, please try to parse invalid JSON, similar like this:  
`'["123":123]'`

---

<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:** [April 3, 2019, 2:26pm UTC](https://discuss.elastic.co/t/json-parse-return-filled-with-null/175175/4 "2019-04-03T14:26:43Z")

</div>

```
event.set("a", jsonParse("[\"123\":123]", event))

```

gets me "a" =\> "["123":123]" with a "\_parseJsonFail" tag.

---

<div class="post-metadata">

**Author:** ![ep4sh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ep4sh/32/40099_2.png) [@ep4sh](https://discuss.elastic.co/u/ep4sh)\
**Post date:** [April 3, 2019, 2:38pm UTC](https://discuss.elastic.co/t/json-parse-return-filled-with-null/175175/5 "2019-04-03T14:38:56Z")

</div>

I have created for additional "debug level" this method:

```
def jsonParse(str, ev)
    invalid = str
    if invalid then
    ev.set("X1",invalid)
else
    ev.set("X2-invalid",invalid)
    end
    begin
        parsed = JSON.parse(str)
    rescue => e
        ev.tag("_parseJsonFail")
        ev.set("X3",parsed)
        ev.set("X4",invalid)
        return invalid
    end
    return parsed
end

```

![image](https://us1.discourse-cdn.com/elastic/original/3X/f/a/fae73c1d218df70342415bf1529548f7bc295ca0.png)  
X1 doesnt exist 😃  
Is it a curse? 😃

---

<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:** [April 3, 2019, 2:42pm UTC](https://discuss.elastic.co/t/json-parse-return-filled-with-null/175175/6 "2019-04-03T14:42:12Z")

</div>

The evidence very much suggests you are calling

```
event.set("cc", jsonParse(nil, event))
```

---

<div class="post-metadata">

**Author:** ![ep4sh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ep4sh/32/40099_2.png) [@ep4sh](https://discuss.elastic.co/u/ep4sh)\
**Post date:** [April 3, 2019, 2:48pm UTC](https://discuss.elastic.co/t/json-parse-return-filled-with-null/175175/7 "2019-04-03T14:48:21Z")

</div>

yep, but it doesn't work **only** when I call invalid JSON  
when valid - parsing Ok  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/9/e/9e45b3861f4458657834149d4175222a462c3f88.png)

maybe I have a misunderstanding?  
invalid JSON:  
`'["abc" : 123]' == nil`  
?

---

<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:** [April 3, 2019, 3:16pm UTC](https://discuss.elastic.co/t/json-parse-return-filled-with-null/175175/8 "2019-04-03T15:16:29Z")

</div>

But ["vasya@example.com", "valera@example.com"] is an example of invalid JSON. It requires {} around it to be parseable.

---

<div class="post-metadata">

**Author:** ![ep4sh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ep4sh/32/40099_2.png) [@ep4sh](https://discuss.elastic.co/u/ep4sh)\
**Post date:** [April 3, 2019, 3:22pm UTC](https://discuss.elastic.co/t/json-parse-return-filled-with-null/175175/9 "2019-04-03T15:22:57Z")

</div>

I checked code by the:

> **[The JSON Validator](https://jsonlint.com/)**
>
> JSONLint is the free online validator and reformatter tool for JSON, a lightweight data-interchange format.

  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/e/0/e0b86470b1e30b36b4c0f4b87e4617c7aac86bfb.png)  
is it wrong?

---

<div class="post-metadata">

**Author:** ![ep4sh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ep4sh/32/40099_2.png) [@ep4sh](https://discuss.elastic.co/u/ep4sh)\
**Post date:** [April 3, 2019, 3:35pm UTC](https://discuss.elastic.co/t/json-parse-return-filled-with-null/175175/10 "2019-04-03T15:35:24Z")

</div>

@Badger Thanks a lot you were right!!  
When i pass to logs:  
`{"aaa":"123"}`  
It breaks  
So it should be passed (with spaces):  
`{"aaa" : "123"}`  
magic! Need some time to solve it 🙂  
Anyway, shake your hand! Thanks for your help!

---

<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 1, 2019, 3:35pm UTC](https://discuss.elastic.co/t/json-parse-return-filled-with-null/175175/11 "2019-05-01T15:35:27Z")

</div>

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