# Extract TAG values from hosts tags in DQL

**URL:** <https://community.dynatrace.com/t/extract-tag-values-from-hosts-tags-in-dql/40394>\
**Category:** DQL\
**Tags:** dql\
**Created:** [June 26, 2023, 8:24pm UTC](https://community.dynatrace.com/t/extract-tag-values-from-hosts-tags-in-dql/40394 "2023-06-26T20:24:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidePiras](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/davidepiras/32/29341_2.png) [@DavidePiras](https://community.dynatrace.com/u/DavidePiras)\
**Post date:** [June 26, 2023, 8:24pm UTC](https://community.dynatrace.com/t/extract-tag-values-from-hosts-tags-in-dql/40394/1 "2023-06-26T20:24:13Z")

</div>

I am querying the list of hosts in our environment, i would need to also filter by presence of a tag and to see the tag name and value in a column of the resulting table,

I am using this query to start with:

fetch dt.entity.host  
| fieldsAdd entity.type, tags  
| filter matchesPhrase(tags,“Business Segment”)

the fact is that&nbsp;Business Segment tag can have multiple values, like Sales, IT, Finance, and each hosts has may tags and what i need is to extract the value of the&nbsp;Business Segment tag into a separated column, to see a grid showing only IT, Finance or Sales in a column, after the host name and the host id,

how can I do this?

Thanks,

Davide

---

<div class="post-metadata">

**Author:** ![sinisa\_zubic](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/sinisa_zubic/32/32498_2.png) [@sinisa\_zubic](https://community.dynatrace.com/u/sinisa_zubic)\
**Post date:** [June 26, 2023, 8:44pm UTC](https://community.dynatrace.com/t/extract-tag-values-from-hosts-tags-in-dql/40394/2 "2023-06-26T20:44:39Z")

</div>

Hi&nbsp;[@DavidePiras](https://community.dynatrace.com/user/39859)

the field tags is an array of multiple tags. You need to use the [expand command](https://www.dynatrace.com/support/help/shortlink/dql-commands#expand)&nbsp;to expand it and then parse the key value information from the tag itself.

```markup
fetch dt.entity.host
| fieldsAdd entity.type,tags
| expand tags
| parse tags, """((LD:tag (!<<'\\' ':') LD:value)|LD:tag)"""
| fieldsAdd tagvalue = if(isNotNull(value), value)
| fieldsRemove value
| fields id, entity.type,tag, tagvalue

```

The parsing looks a bit complex, but it is already optimized for some edge cases I have observed so far.

Best,  
Sini

---

<div class="post-metadata">

**Author:** ![DavidePiras](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/davidepiras/32/29341_2.png) [@DavidePiras](https://community.dynatrace.com/u/DavidePiras)\
**Post date:** [June 26, 2023, 9:02pm UTC](https://community.dynatrace.com/t/extract-tag-values-from-hosts-tags-in-dql/40394/3 "2023-06-26T21:02:22Z")

</div>

Amazing, thank you so much for the hint, this is my adapted final query and works quite well, appreciated your prompt answer Sinisa!

**fetch dt.entity.host  
| fieldsAdd grp=instance\_of[dt.entity.host\_group]  
| fieldsAdd tags  
| fieldsAdd monitoringMode  
| expand tags  
| parse tags, “”“((LD:tag (!\<\<‘\\’ ‘:’) LD:value)|LD:tag)”“”  
| fieldsAdd tagvalue = if(isNotNull(value), value)  
| fieldsRemove value  
| lookup [fetch dt.entity.host\_group],sourceField:grp,lookupField:id  
| fields HostGroup=lookup.entity.name, HostName=entity.name, tags, monitoringMode, tag, BusinessSegment=tagvalue  
| filter isNotNull(HostGroup)  
| filter tag == “Business Segment”  
| sort HostGroup ASC, HostName ASC**

---

<div class="post-metadata">

**Author:** ![SaiK](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@SaiK](https://community.dynatrace.com/u/SaiK)\
**Post date:** [September 2, 2024, 2:37pm UTC](https://community.dynatrace.com/t/extract-tag-values-from-hosts-tags-in-dql/40394/4 "2024-09-02T14:37:53Z")

</div>

Hello, I believe this is quite old comment but I am new to Dynatrace and was wondering if there are any documentation/guide available to help me understand the parsing command better that was used in the query here, thank you.
