# Converting a json field to a list json

**URL:** <https://discuss.elastic.co/t/converting-a-json-field-to-a-list-json/78683>\
**Category:** Logstash\
**Created:** [March 15, 2017, 10:36am UTC](https://discuss.elastic.co/t/converting-a-json-field-to-a-list-json/78683 "2017-03-15T10:36:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![E.Eric](https://avatars.discourse-cdn.com/v4/letter/e/dec6dc/32.png) [@E.Eric](https://discuss.elastic.co/u/E.Eric)\
**Post date:** [March 15, 2017, 10:36am UTC](https://discuss.elastic.co/t/converting-a-json-field-to-a-list-json/78683/1 "2017-03-15T10:36:40Z")

</div>

Hi,

having a field:

"aa": {  
"b":"123",  
"c":"123"  
}

I want to convert that field to a list JSON with one object:

"aa": [  
{  
"b":"123",  
"c":"123"  
}  
]

Thanks in advance!

---

<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:** [March 15, 2017, 12:06pm UTC](https://discuss.elastic.co/t/converting-a-json-field-to-a-list-json/78683/2 "2017-03-15T12:06:19Z")

</div>

I think you need to use a ruby filter for this. This piece of code probably works:

```
event.set('aa', [event.get('aa')])

```

This seems like a rather strange transform. Why do you want to do this?

---

<div class="post-metadata">

**Author:** ![E.Eric](https://avatars.discourse-cdn.com/v4/letter/e/dec6dc/32.png) [@E.Eric](https://discuss.elastic.co/u/E.Eric)\
**Post date:** [March 15, 2017, 1:56pm UTC](https://discuss.elastic.co/t/converting-a-json-field-to-a-list-json/78683/3 "2017-03-15T13:56:04Z")

</div>

I need this because I have a module that is waiting for a list json to parse it and extract data.  
I've trayed this filter :

ruby {  
code =\> "  
require 'json'  
event['res'] = [JSON.parse(event['message'])['aa']]  
"  
}  
but it generates this error:

Ruby exception occurred: can't convert Hash into String {:level=\>:error}

---

<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:** [March 15, 2017, 1:58pm UTC](https://discuss.elastic.co/t/converting-a-json-field-to-a-list-json/78683/4 "2017-03-15T13:58:03Z")

</div>

Why would you need do any JSON conversion? Did you try my suggestion? Which version of Logstash?

---

<div class="post-metadata">

**Author:** ![E.Eric](https://avatars.discourse-cdn.com/v4/letter/e/dec6dc/32.png) [@E.Eric](https://discuss.elastic.co/u/E.Eric)\
**Post date:** [March 15, 2017, 2:00pm UTC](https://discuss.elastic.co/t/converting-a-json-field-to-a-list-json/78683/5 "2017-03-15T14:00:26Z")

</div>

it generates an error : version 2.3

---

<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:** [March 15, 2017, 2:05pm UTC](https://discuss.elastic.co/t/converting-a-json-field-to-a-list-json/78683/6 "2017-03-15T14:05:48Z")

</div>

For 2.3 this should work:

```
event['aa'] = [event['aa']]
```

---

<div class="post-metadata">

**Author:** ![E.Eric](https://avatars.discourse-cdn.com/v4/letter/e/dec6dc/32.png) [@E.Eric](https://discuss.elastic.co/u/E.Eric)\
**Post date:** [March 15, 2017, 2:08pm UTC](https://discuss.elastic.co/t/converting-a-json-field-to-a-list-json/78683/7 "2017-03-15T14:08:44Z")

</div>

it works... Thank you very much !

---

<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 12, 2017, 2:09pm UTC](https://discuss.elastic.co/t/converting-a-json-field-to-a-list-json/78683/8 "2017-04-12T14:09:20Z")

</div>

Closed on April 12, 2017, 2:09pm UTC

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