# Reading attributes and tags

**URL:** <https://forums.losant.com/t/reading-attributes-and-tags/810>\
**Category:** Help\
**Created:** [March 15, 2018, 3:09am UTC](https://forums.losant.com/t/reading-attributes-and-tags/810 "2018-03-15T03:09:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_lamborn](https://avatars.discourse-cdn.com/v4/letter/t/c5a1d2/32.png) [@Tim\_lamborn](https://forums.losant.com/u/Tim_lamborn)\
**Post date:** [March 15, 2018, 3:09am UTC](https://forums.losant.com/t/reading-attributes-and-tags/810/1 "2018-03-15T03:09:52Z")

</div>

Hi,

Can you please provide examples of how to fetch a devices attributes and tags in both javascript and string  
templates?

For example I want to fetch the value of a tag called Site in a string template.

`{{valueByKey data.sigfoxDevice.tags ‘Site’}}` \<-- doesn’t seem to work

Also in code:

```auto
var site = "No Site Id";
for (var i in payload.data.sigfoxDevice.tags)
{
  var tag = payload.data.sigfoxDevice.tags[i];
  if(tag.key == "Site")
  {
    site = tag.value;
  }
}

```

Which works but I wonder if there is an easier way?

Thanks

Ashley

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [March 15, 2018, 1:09pm UTC](https://forums.losant.com/t/reading-attributes-and-tags/810/2 "2018-03-15T13:09:43Z")

</div>

Two things to consider here before we get to the answer …

1. [Device tags](https://docs.losant.com/devices/overview/#device-tags) can have multiple values per tag. i.e. In your device config, you can set a tag of “a=b” and another tag of “a=c”, and when querying the device, you will be returned multiple tags with a key of “a”: one for each unique value.
2. Depending on how the device got onto your payload, the format of your tags could be different.

If you used the [Device: Get Node](https://docs.losant.com/workflows/data/get-device/) with the “Return tags as an object map instead of an array” checkbox **unchecked** , or if you queried a device using the “Device: Get” call in the [Losant API Node](https://docs.losant.com/workflows/data/losant-api/), your tags will be on your payload in a format like the following …

```auto
{
    "myDevice": {
        "tags": [
            {
                "key": "a",
                "value": "b"
            },
            {
                "key": "a",
                "value": "c"
            },
            {
                "key": "foo",
                "value": "bar"
            }
        ]
    }
}

```

So to get the value of the tag with the key `foo`, you could use the [string template](https://docs.losant.com/workflows/accessing-payload-data/#string-templates) `{{valueByKey myDevice.tags 'foo' 'key' 'value'}}`. This string template will return the **first** value that it hits, so it would return “b” if you were trying to get the value of the tag `a`.

If the device got onto your payload through any other method (e.g. the [Device: State Trigger](https://docs.losant.com/workflows/triggers/device/), any of the other device triggers, or the Device: Get Node with the object map checkbox checked), the tags will be in a format like the following:

```auto
{
    "myDevice": {
        "tags": {
            "a": ["b", "c"],
            "foo": ["bar"]
        }
    }
}

```

The value(s) of each tag is always returned as an array, even if there is only one value for the tag.

Here, to get the value of the tag `foo`, you would use the string template `{{myDevice.tags.foo.[0]}}`. The `0` here represents the index of the value you would like; for example, if you wanted the second value of the tag `a`, this string template would be `{{myDevice.tags.a.[1]}}`.

As for how to do either in JavaScript, I don’t think there’s a good method I can recommend other than running a `for` loop like you did in your example. We don’t expose any utility belt libraries for use in the [Function Node](https://docs.losant.com/workflows/logic/function/).

---

<div class="post-metadata">

**Author:** ![Tim\_lamborn](https://avatars.discourse-cdn.com/v4/letter/t/c5a1d2/32.png) [@Tim\_lamborn](https://forums.losant.com/u/Tim_lamborn)\
**Post date:** [March 15, 2018, 7:58pm UTC](https://forums.losant.com/t/reading-attributes-and-tags/810/3 "2018-03-15T19:58:38Z")

</div>

Great explanation. Thanks for the awesome support as usual…
