# JSON Array To Object

**URL:** <https://discuss.elastic.co/t/json-array-to-object/147606>\
**Category:** Logstash\
**Created:** [September 6, 2018, 3:42pm UTC](https://discuss.elastic.co/t/json-array-to-object/147606 "2018-09-06T15:42:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![manwithoutsleep](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@manwithoutsleep](https://discuss.elastic.co/u/manwithoutsleep)\
**Post date:** [September 6, 2018, 3:42pm UTC](https://discuss.elastic.co/t/json-array-to-object/147606/1 "2018-09-06T15:42:15Z")

</div>

Hi -

Each record of input to my Logstash instance is a JSON array of key-value pairs. I need to translate this array to a single object with the value of the "key" as the field name and the value of the "value" as the field value. For example, this is a single record of input:

```
[
	{"Name":"FirstName","Value":"Jamie"},
	{"Name":"LastName","Value":"Smith"},
	{"Name":"Email","Value":"test@value.com"},
	{"Name":"State","Value":"MO"}
]

```

And this is the ElasticSearch mapping to which this record needs to fit:

```
{
	"data-test": {
		"mappings": {
			"_doc": {
				"properties": {
					"FirstName": { "type": "text" },
					"LastName": { "type": "text" },
					"Email": { "type": "text" },
					"State": { "type": "text" }
				}
			}
		}
	}
}

```

I've been looking at a series of filters (kv, translate, split, etc.) but have not seen an easy way to make this happen reliably.

Any guidance or wisdom would be greatly appreciated!

Thanks -  
Brad

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [September 6, 2018, 8:17pm UTC](https://discuss.elastic.co/t/json-array-to-object/147606/2 "2018-09-06T20:17:31Z")

</div>

You need a ruby filter. Something like this should work:

```ruby
event.get('name-of-array-field').each { |kv|
  event.set(kv['Name'], kv['Value'])
}

```

---

<div class="post-metadata">

**Author:** ![manwithoutsleep](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@manwithoutsleep](https://discuss.elastic.co/u/manwithoutsleep)\
**Post date:** [September 7, 2018, 3:46pm UTC](https://discuss.elastic.co/t/json-array-to-object/147606/3 "2018-09-07T15:46:36Z")

</div>

This looks promising, but so far no luck. I don't have a name on the array, so I'm not sure how to access it via the event getter. Unfortunately the data is what it is, I can't modify it. The top level element of an event is the array, the elements of the array are the fields themselves. Is there a way for the event object to return the elements of the top-level array without the array itself having a name?

Thanks -  
Brad

---

<div class="post-metadata">

**Author:** ![manwithoutsleep](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@manwithoutsleep](https://discuss.elastic.co/u/manwithoutsleep)\
**Post date:** [September 7, 2018, 5:55pm UTC](https://discuss.elastic.co/t/json-array-to-object/147606/4 "2018-09-07T17:55:30Z")

</div>

Ah, got it! I ended up applying first a json filter, then the ruby filter, like this:

```
filter {
    json {
        source => "loan_object"
        target => "parsed_object"
        remove_field => "loan_object"
    }
    ruby {
        code => "event.get('parsed_object').each { |kv| event.set(kv['Name'], kv['Value']) }"
    }
}

```

Thanks much for the assistance!  
Brad

---

<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:** [October 5, 2018, 5:55pm UTC](https://discuss.elastic.co/t/json-array-to-object/147606/5 "2018-10-05T17:55:33Z")

</div>

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