# Variable into JSON object path?

**URL:** <https://forums.losant.com/t/variable-into-json-object-path/1942>\
**Category:** Help\
**Tags:** workflow\
**Created:** [July 2, 2019, 4:55am UTC](https://forums.losant.com/t/variable-into-json-object-path/1942 "2019-07-02T04:55:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jose\_Cruz](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/jose_cruz/32/1580_2.png) [@Jose\_Cruz](https://forums.losant.com/u/Jose_Cruz)\
**Post date:** [July 2, 2019, 4:55am UTC](https://forums.losant.com/t/variable-into-json-object-path/1942/1 "2019-07-02T04:55:43Z")

</div>

Greetings. Is there an easy way to insert a variable as part of a new payload path object? For example :

> 1 var dev = payload.data.message.dev\_id  
> 2 // Extract Device ID last 4 digits to use for identifying variables  
> 3 var eui = dev.substr(12,4)  
> 4 // Declare variables, including the decoding ‘payload\_raw’ to HEX:  
> 5 var raw = payload.data.message.payload\_raw  
> 6 var buf = new Buffer(raw, ‘base64’)  
> 7 var hex = buf.toString(‘hex’)  
> 8 // Now, extract how many bytes in the payload and add to a ‘payload\_cnt’ variable:  
> 9 var cnt = hex.length  
> 10 // Add these new objects to the new payload variables :  
> 11 payload.data.message.device\_id = eui  
> 12 payload.data.message.payload\_hex = hex  
> 13 payload.data.message.payload\_cnt = cnt

The above works so far, but I would like in line 12 and 13 for ‘device\_id’ / eui to be included. For example, if eui = ‘be76’ then path for line 12 would read **payload.data.message.payload\_hex\_be76** or similar. Line 13 output would read **payload.data.message.payload\_cnt\_be76** or similar.

Thanks, j

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [July 2, 2019, 2:48pm UTC](https://forums.losant.com/t/variable-into-json-object-path/1942/2 "2019-07-02T14:48:01Z")

</div>

Hi @Jose_Cruz!

We can solve this with some JavaScript magic ✨

In JavaScript, you can also reference the object using the array syntax.

Let’s say you had something like this:

```auto
{
   payload: {
       data: "Something"
  }

}

```

You can access data like this:

`payload.data`

or like this:

`payload['data']`

Being that to be true, you can add new variables like this:

```auto
var key = 'payload_hex_be76'
payload[key] = '0AFF'
console.log(payload.payload_hex_be76) // prints 0AFF

```

---

<div class="post-metadata">

**Author:** ![Jose\_Cruz](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/jose_cruz/32/1580_2.png) [@Jose\_Cruz](https://forums.losant.com/u/Jose_Cruz)\
**Post date:** [July 2, 2019, 3:18pm UTC](https://forums.losant.com/t/variable-into-json-object-path/1942/3 "2019-07-02T15:18:34Z")

</div>

Awesome! Let me try work/learn someof this **magic** then! Thank you for your prompt reply 😃 ! 🔴

---

<div class="post-metadata">

**Author:** ![Jose\_Cruz](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/jose_cruz/32/1580_2.png) [@Jose\_Cruz](https://forums.losant.com/u/Jose_Cruz)\
**Post date:** [July 6, 2019, 4:16am UTC](https://forums.losant.com/t/variable-into-json-object-path/1942/4 "2019-07-06T04:16:26Z")

</div>

It took me a cpl of days with its nights, some 🍻 and 🎆 , but I finally got it following your lead @anaptfox. This did the trick :

> var dev = payload.data.message.dev\_id  
> var eui = dev.substr(12,4)  
> var pth = “payload\_hex\_”  
> payload.data.message[pth + eui] = hex

payload now is assigned with the dynamic ‘eui’ variable (dev\_id) of the device, in this case ‘be76’ :

> - “payload\_hex\_be76”:“1f0200”

Thank you again for the 🧙 tip!
