# Add\_field with a name generated from message data

**URL:** <https://discuss.elastic.co/t/add-field-with-a-name-generated-from-message-data/182375>\
**Category:** Logstash\
**Created:** [May 23, 2019, 8:24am UTC](https://discuss.elastic.co/t/add-field-with-a-name-generated-from-message-data/182375 "2019-05-23T08:24:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrihds](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jrihds](https://discuss.elastic.co/u/jrihds)\
**Post date:** [May 23, 2019, 8:24am UTC](https://discuss.elastic.co/t/add-field-with-a-name-generated-from-message-data/182375/1 "2019-05-23T08:24:43Z")

</div>

Hello,

Is it possible to add a new field whose name is based on data from the message? e.g.

add\_field =\> {  
"%{[parsed-json][0][key]}" =\> "%{[parsed-json][0][value]}"  
}

I am currently getting an 'Invalid FieldReference' error.

For completeness, the following config works successfully but doesn't achieve what I need:

add\_field =\> {  
"value-field" =\> "%{[parsed-json][0][value]}"  
"key-field" =\> "%{[parsed-json][0][key]}"  
}

Thank you

---

<div class="post-metadata">

**Author:** ![jrihds](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jrihds](https://discuss.elastic.co/u/jrihds)\
**Post date:** [May 23, 2019, 9:19am UTC](https://discuss.elastic.co/t/add-field-with-a-name-generated-from-message-data/182375/2 "2019-05-23T09:19:06Z")

</div>

I worked out a solution. I needed to add validation to ensure that the data exists before I try and create a field for it, as below:

if [parsed-json][0] {  
mutate {  
add\_field =\> {  
"%{[parsed-json][0][key]}" =\> "%{[parsed-json][0][value]}"  
}  
}  
}

---

<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:** [June 20, 2019, 9:19am UTC](https://discuss.elastic.co/t/add-field-with-a-name-generated-from-message-data/182375/3 "2019-06-20T09:19:16Z")

</div>

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