# Convert value for nested json array

**URL:** https://discuss.elastic.co/t/convert-value-for-nested-json-array/104672
**Category:** Logstash
**Created:** [October 20, 2017, 8:10am UTC](https://discuss.elastic.co/t/convert-value-for-nested-json-array/104672 "2017-10-20T08:10:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Danilo](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@Danilo](https://discuss.elastic.co/u/Danilo)
#### Post date: [October 20, 2017, 8:10am UTC](https://discuss.elastic.co/t/convert-value-for-nested-json-array/104672/1 "2017-10-20T08:10:26Z")

</div>

Hello,  
I have a json input like this one:  
{  
"fieldA" : "valueA",  
"fieldB" : [{ "nestedstring" : "string1a", "nestedint": number1b }, { "nestedstring" : "string2a", "nestedint": number2b }, ...]  
}

I would like to convert the type of all "nestedint" from number (long integer) to string ... I have made some test like:  
ruby {  
code =\> "  
event.get('fieldB').each { |k|  
k['nestedint'] = k['nestedint'].to\_s  
}  
"  
}

But ... without any result (and without any error) ... how can I solve my problem?

Thanks in advance

---

<div class="post-metadata">

### Author: ![paz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paz/32/28003_2.png) [@paz](https://discuss.elastic.co/u/paz)
#### Post date: [October 20, 2017, 1:28pm UTC](https://discuss.elastic.co/t/convert-value-for-nested-json-array/104672/2 "2017-10-20T13:28:29Z")

</div>

Well, there are a couple issues with the above:

1. You need to use the _.set_ method to alter a field inside a Ruby block.
2. You need to provide a full tree path on the _.set_ method to change that field.

A solution would be to iterate with index and use that to construct the full path, like so

```auto
    ruby {
        code => "
            event.get('fieldB').each_index { |i|
                event.set('[fieldB]['+i.to_s+'][nestedint]', event.get('[fieldB]['+i.to_s+'][nestedint]').to_s)
            }
        "
    }
```

---

<div class="post-metadata">

### Author: ![Danilo](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@Danilo](https://discuss.elastic.co/u/Danilo)
#### Post date: [October 23, 2017, 6:54am UTC](https://discuss.elastic.co/t/convert-value-for-nested-json-array/104672/3 "2017-10-23T06:54:23Z")

</div>

Thank you!

---

<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: [November 20, 2017, 6:54am UTC](https://discuss.elastic.co/t/convert-value-for-nested-json-array/104672/4 "2017-11-20T06:54:57Z")

</div>

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