# Pull values from array of json

**URL:** <https://discuss.elastic.co/t/pull-values-from-array-of-json/175900>\
**Category:** Logstash\
**Created:** [April 8, 2019, 6:06pm UTC](https://discuss.elastic.co/t/pull-values-from-array-of-json/175900 "2019-04-08T18:06:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_MacPherson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_macpherson/32/43695_2.png) [@Michael\_MacPherson](https://discuss.elastic.co/u/Michael_MacPherson)\
**Post date:** [April 8, 2019, 6:06pm UTC](https://discuss.elastic.co/t/pull-values-from-array-of-json/175900/1 "2019-04-08T18:06:36Z")

</div>

Hi,

I have the following array as a field in my event and I am looking to pull out the value from name as a list.

For example:

{colour=#f26211, exportable=true, numerical\_value=null, hide\_tag=false, user\_id=0, name=tag1, id=9},{colour=#11d7f2, exportable=true, numerical\_value=null, hide\_tag=false, user\_id=0, name=tag2, id=11}

Return:  
['tag1','tag2']

---

<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 8, 2019, 6:33pm UTC](https://discuss.elastic.co/t/pull-values-from-array-of-json/175900/2 "2019-04-08T18:33:00Z")

</div>

Are you saying that one of the fields on your event contains that entire string? Or are you saying it is an object?

---

<div class="post-metadata">

**Author:** ![Michael\_MacPherson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_macpherson/32/43695_2.png) [@Michael\_MacPherson](https://discuss.elastic.co/u/Michael_MacPherson)\
**Post date:** [April 8, 2019, 6:34pm UTC](https://discuss.elastic.co/t/pull-values-from-array-of-json/175900/3 "2019-04-08T18:34:22Z")

</div>

Yes one of the fields in the event contains that as the entire string.

---

<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 8, 2019, 6:40pm UTC](https://discuss.elastic.co/t/pull-values-from-array-of-json/175900/4 "2019-04-08T18:40:30Z")

</div>

> [@Michael\_MacPherson](#):
>
> {colour=#f26211, exportable=true, numerical\_value=null, hide\_tag=false, user\_id=0, name=tag1, id=9},{colour=#11d7f2, exportable=true, numerical\_value=null, hide\_tag=false, user\_id=0, name=tag2, id=11}

```
    mutate { gsub => ["someField", "},{", "}|{"] }
    mutate { split => { "someField" => "|" } }
    kv { source => "someField" target => "[@metadata][stuff]" }
    mutate { copy => { "[@metadata][stuff][name]" => "name" } }

```

kv will iterate over an array and extract kv pairs into arrays.

```
 "someField" => [
    [0] "{colour=#f26211, exportable=true, numerical_value=null, hide_tag=false, user_id=0, name=tag1, id=9}",
    [1] "{colour=#11d7f2, exportable=true, numerical_value=null, hide_tag=false, user_id=0, name=tag2, id=11}"
],
      "name" => [
    [0] "tag1,",
    [1] "tag2,"
],

```

---

<div class="post-metadata">

**Author:** ![Michael\_MacPherson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_macpherson/32/43695_2.png) [@Michael\_MacPherson](https://discuss.elastic.co/u/Michael_MacPherson)\
**Post date:** [April 8, 2019, 6:47pm UTC](https://discuss.elastic.co/t/pull-values-from-array-of-json/175900/5 "2019-04-08T18:47:42Z")

</div>

Awesome, this works...thanks for the help

---

<div class="post-metadata">

**Author:** ![BennyInc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bennyinc/32/21751_2.png) [@BennyInc](https://discuss.elastic.co/u/BennyInc)\
**Post date:** [April 9, 2019, 9:41am UTC](https://discuss.elastic.co/t/pull-values-from-array-of-json/175900/6 "2019-04-09T09:41:47Z")

</div>

Parsing json with a gsub/split feels "wrong" somehow. I wonder whether this might break in some dramatic way, e.g. if your json contains whitespaces or newlines between fields.

The "cleaner" solution might be to use the [JSON filter](https://www.elastic.co/guide/en/logstash/current/plugins-filters-json.html) to parse the field into another field that then contains subfields (using the [target parameter](https://www.elastic.co/guide/en/logstash/current/plugins-filters-json.html#plugins-filters-json-target)), and then accessing the name field within that target with the [mutate-copy](https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html#plugins-filters-mutate-copy) to pull it into your final destination. After that you can remove the json target again.

---

<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 9, 2019, 11:31am UTC](https://discuss.elastic.co/t/pull-values-from-array-of-json/175900/7 "2019-04-09T11:31:47Z")

</div>

> [@BennyInc](#):
>
> The "cleaner" solution might be to use the [JSON filter](https://www.elastic.co/guide/en/logstash/current/plugins-filters-json.html)

It's not valid JSON.

---

<div class="post-metadata">

**Author:** ![BennyInc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bennyinc/32/21751_2.png) [@BennyInc](https://discuss.elastic.co/u/BennyInc)\
**Post date:** [April 9, 2019, 1:35pm UTC](https://discuss.elastic.co/t/pull-values-from-array-of-json/175900/8 "2019-04-09T13:35:23Z")

</div>

> [@Badger](#):
>
> It's not valid JSON.

Oh, right. I somehow missed that.

---

<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 7, 2019, 1:35pm UTC](https://discuss.elastic.co/t/pull-values-from-array-of-json/175900/9 "2019-05-07T13:35:25Z")

</div>

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