# How to assign one value to another

**URL:** https://discuss.elastic.co/t/how-to-assign-one-value-to-another/268824
**Category:** Logstash
**Created:** [March 30, 2021, 4:50pm UTC](https://discuss.elastic.co/t/how-to-assign-one-value-to-another/268824 "2021-03-30T16:50:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Pallavibhushan](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@Pallavibhushan](https://discuss.elastic.co/u/Pallavibhushan)
#### Post date: [March 30, 2021, 4:50pm UTC](https://discuss.elastic.co/t/how-to-assign-one-value-to-another/268824/1 "2021-03-30T16:50:37Z")

</div>

Hi ,  
I am new to logstash  
I have below data in JSON.  
"attributes":  
[  
{ "name": "test1",  
"value": "test1\_value" },  
{ "name": "test2",  
"value": "test2\_value" }  
]  
I want this data in below format :  
"attributes":  
[  
{ "test1": "test1\_value" },  
{ "test2": "test2\_value" }  
]  
how can we do this in logstash config?

---

<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: [March 30, 2021, 5:16pm UTC](https://discuss.elastic.co/t/how-to-assign-one-value-to-another/268824/2 "2021-03-30T17:16:33Z")

</div>

> [@Pallavibhushan](#):
>
> how can we do this in logstash config?

You would do it in a ruby filter. I have not tested it, but you could try this:

```
ruby {
    code => '
        a = event.get("attributes")
        if a.is_a? Array
            newA = []
            a.each { |x|
                 newA << { x["name"] => x["value"] }
            }
            event.set("attributes", newA)
        end
    '
}

```

---

<div class="post-metadata">

### Author: ![Pallavibhushan](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@Pallavibhushan](https://discuss.elastic.co/u/Pallavibhushan)
#### Post date: [March 30, 2021, 5:58pm UTC](https://discuss.elastic.co/t/how-to-assign-one-value-to-another/268824/3 "2021-03-30T17:58:26Z")

</div>

Hi @Badger ,  
Thanks for your response.  
My input Json is something like this .  
{  
"field1":value1,  
"attributes": [{ "name": "test1", "value": "test\_value1" },{ "name": "test2", "value": "test\_value2" }]  
}  
and config as below:

input {  
file  
{  
type =\> "json"  
codec =\> "json"  
path =\> "C:/logstash-7.6.0/input/sample\*.json"  
start\_position =\> "beginning"  
}  
}  
filter {

```
		mutate {
			add_field => { 
									"sample_Number"=>"%{[field1]}"									
									"Attributes"=>"%{[attributes]}"
									
							}
				}

```

ruby {  
code =\> '  
a = event.get("Attributes")  
if a.is\_a? json  
newA = {}  
a.each { |x|  
newA \<\< { x["name"] =\> x["value"] }  
}  
event.set("Attributes", newA)  
end  
'  
}

```
			prune {
						whitelist_names =>["sample_Number","Attributes"]
  					}
}

```

output {  
stdout { }  
elasticsearch {  
index =\> "test\_index"  
hosts =\> ["[http://localhost:9200/](http://localhost:9200/)"]  
action =\> "create"  
}   
}

It is not working as expected.

---

<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: [March 30, 2021, 6:19pm UTC](https://discuss.elastic.co/t/how-to-assign-one-value-to-another/268824/4 "2021-03-30T18:19:51Z")

</div>

> [@Pallavibhushan](#):
>
> if a.is\_a? json

This will never be true, so the ruby code will not do anything. Attributes is an Array.

---

<div class="post-metadata">

### Author: ![Pallavibhushan](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@Pallavibhushan](https://discuss.elastic.co/u/Pallavibhushan)
#### Post date: [March 31, 2021, 5:21am UTC](https://discuss.elastic.co/t/how-to-assign-one-value-to-another/268824/5 "2021-03-31T05:21:55Z")

</div>

I tried with if a.is\_a? Array. but still same issue  
It is passing entire json as it is.

---

<div class="post-metadata">

### Author: ![Pallavibhushan](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@Pallavibhushan](https://discuss.elastic.co/u/Pallavibhushan)
#### Post date: [March 31, 2021, 8:52am UTC](https://discuss.elastic.co/t/how-to-assign-one-value-to-another/268824/6 "2021-03-31T08:52:13Z")

</div>

it worked now.  
issue was ..  
before below code, I was copying data from attribute field to another fields. after removing that it worked  
ruby {  
code =\> '

```
    a = event.get("attributes")
    if a.is_a? Array 
        newA = []
        a.each do |x|
             newA << { x["name"] => x["value"] }
        end
        event.set("attributes", newA)
		
    end
'

```

}

Thanks @Badger

---

<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 28, 2021, 8:52am UTC](https://discuss.elastic.co/t/how-to-assign-one-value-to-another/268824/7 "2021-04-28T08:52:14Z")

</div>

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