# Pass the Result of DQL Query in Workflow

**URL:** <https://community.dynatrace.com/t/pass-the-result-of-dql-query-in-workflow/48233>\
**Category:** Automations\
**Tags:** dql, workflows\
**Created:** [October 9, 2024, 2:00pm UTC](https://community.dynatrace.com/t/pass-the-result-of-dql-query-in-workflow/48233 "2024-10-09T14:00:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicemukesh](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@nicemukesh](https://community.dynatrace.com/u/nicemukesh)\
**Post date:** [October 9, 2024, 2:00pm UTC](https://community.dynatrace.com/t/pass-the-result-of-dql-query-in-workflow/48233/1 "2024-10-09T14:00:00Z")

</div>

While working with Workflows, I noticed that the Result that is returned from a TASK1 DQL Query in a workflow is in a JSON format, and when I try passing that onto another DQL query, it gives an error.

So i would like to know how to pass result of task1 to next task1 in workflow and also how to parse it?

---

<div class="post-metadata">

**Author:** ![StrangerThing](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/strangerthing/32/12182_2.png) [@StrangerThing](https://community.dynatrace.com/u/StrangerThing)\
**Post date:** [October 9, 2024, 2:24pm UTC](https://community.dynatrace.com/t/pass-the-result-of-dql-query-in-workflow/48233/2 "2024-10-09T14:24:04Z")

</div>

I’m not exactly sure what you’re asking about, but you might need to use the expression filters to\_json or from\_json like so:&nbsp; {{result(“task1”)|from\_json}}

---

<div class="post-metadata">

**Author:** ![nicemukesh](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@nicemukesh](https://community.dynatrace.com/u/nicemukesh)\
**Post date:** [October 9, 2024, 2:30pm UTC](https://community.dynatrace.com/t/pass-the-result-of-dql-query-in-workflow/48233/3 "2024-10-09T14:30:00Z")

</div>

Thanks for your reply..

Actually i am passing result from Task1 to Task2 in workflow.&nbsp;Right now, i am getting result of Task1 in JSON format.&nbsp;And trying to parse it so that it would become huma readable format.

---

<div class="post-metadata">

**Author:** ![StrangerThing](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/strangerthing/32/12182_2.png) [@StrangerThing](https://community.dynatrace.com/u/StrangerThing)\
**Post date:** [October 9, 2024, 2:32pm UTC](https://community.dynatrace.com/t/pass-the-result-of-dql-query-in-workflow/48233/4 "2024-10-09T14:32:57Z")

</div>

You can use the parse command in DQL to parse the json into a record with the fields.&nbsp;  
[Extraction and parsing commands - Dynatrace Docs](https://docs.dynatrace.com/docs/platform/grail/dynatrace-query-language/commands/extraction-and-parsing-commands#parse)

---

<div class="post-metadata">

**Author:** ![nicemukesh](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@nicemukesh](https://community.dynatrace.com/u/nicemukesh)\
**Post date:** [October 9, 2024, 2:40pm UTC](https://community.dynatrace.com/t/pass-the-result-of-dql-query-in-workflow/48233/5 "2024-10-09T14:40:08Z")

</div>

This is error coming while running it

Error evaluating ‘query’ in input. the JSON object must be str, bytes or bytearray, not dict Use the expression preview in edit mode to review the input.

---

<div class="post-metadata">

**Author:** ![StrangerThing](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/strangerthing/32/12182_2.png) [@StrangerThing](https://community.dynatrace.com/u/StrangerThing)\
**Post date:** [October 9, 2024, 2:58pm UTC](https://community.dynatrace.com/t/pass-the-result-of-dql-query-in-workflow/48233/6 "2024-10-09T14:58:28Z")

</div>

That’s where you’ll need to use the “to\_json” expression filter like I mentioned earlier. Something like this:&nbsp; {{result(“task1”)|to\_json}}

---

<div class="post-metadata">

**Author:** ![nicemukesh](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@nicemukesh](https://community.dynatrace.com/u/nicemukesh)\
**Post date:** [October 10, 2024, 11:21am UTC](https://community.dynatrace.com/t/pass-the-result-of-dql-query-in-workflow/48233/7 "2024-10-10T11:21:55Z")

</div>

I tried to parse&nbsp;into a record with the fields, but it is not fetching result.

data json:“”“[{{result(“task1”)|to\_json}}]”“”  
| parse content, “JSON:json”  
| fields “Namespace”

I am trying to print an output of TASK 1 into tabular format

---

<div class="post-metadata">

**Author:** ![StrangerThing](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/strangerthing/32/12182_2.png) [@StrangerThing](https://community.dynatrace.com/u/StrangerThing)\
**Post date:** [October 10, 2024, 3:28pm UTC](https://community.dynatrace.com/t/pass-the-result-of-dql-query-in-workflow/48233/8 "2024-10-10T15:28:02Z")

</div>

When viewing your Workflow execution, you can see the result of your “task1”. You may need to get into a field before you actually can access the data returned. For example, you may need to use {{ result(“task1”).records }} to access the “records” field of the result.

It’s hard to determine exactly what you’re trying to do with limited information, so sorry if this doesn’t help you.

---

<div class="post-metadata">

**Author:** ![Maheedhar\_T](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/maheedhar_t/32/155_2.png) [@Maheedhar\_T](https://community.dynatrace.com/u/Maheedhar_T)\
**Post date:** [October 14, 2024, 1:55pm UTC](https://community.dynatrace.com/t/pass-the-result-of-dql-query-in-workflow/48233/9 "2024-10-14T13:55:05Z")

</div>

[@nicemukesh](https://community.dynatrace.com/user/70937)&nbsp;,  
Let me try to explain this with an example.  
The first task is a DQL and the result would obviously be a JSON value.  
What I assume here is from the JSON you are getting you want a particular field to be used in another DQL which would require you to loop through the result of first task.

Just let me try to explain this with an example.  
Here I am taking two DQLs first one gets the hosts which have CPU usage greater than 50%.

```markup
timeseries by:{dt.entity.host}, cpu_usage = avg(dt.host.cpu.usage)
| filter arrayAvg(cpu_usage) > 50
| lookup [fetch dt.entity.host], sourceField:dt.entity.host, lookupField:id, fields:{entity.name}

```

The second DQL gets the host details by host id

```markup
fetch dt.entity.host
| fields id == ""

```

Now I do not want to pass the id as a static or hardcoded value rather I would like to pass host id of the hosts that came up in the first DQL result.

Now the sample result of first DQL would be something like this.

```markup
[
  {
    "status": "NONE",
    "content": "2023-01-01T01:01:01Z localhost proxy[12529]: 10.0.0.10:38440 http-in~ individual_servers/abc 217/0/0/1/218 HTTP_STATUS 200 284 - - --NN 5749/5745/0/1/0 0/0 {|||some.domain.com:443} {|} \"POST /path?param=foo HTTP/1.1\" ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2",
    "content2": "2023-01-01T01:01:01Z localhost proxy[12529]: 10.0.0.10:38440 http-in~ individual_servers/abc 217/0/0/1/218 HTTP_STATUS 200 284 - - --NN 5749/5745/0/1/0 0/0 {|||some.domain.com:443} {|} \"POST /path?param=foo HTTP/1.1\" ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2",
    "loglevel": "NONE",
    "host.name": "HOST-IG-12-34567",
    "timestamp": "2023-01-01T01:01:01.000000001Z",
    "event.type": "LOG",
    "log.source": "/var/spool/some.log",
    "dt.entity.host": "HOST-1234567890",
    "dt.source_entity": "PROCESS_GROUP_INSTANCE-ABCDEF1234567",
    "dt.security_context": "application-a",
    "dt.entity.process_group": "PROCESS_GROUP-ABCDEF1234567",
    "dt.entity.process_group_instance": "PROCESS_GROUP_INSTANCE-ABCDEF1234567"
  },
  {
    "status": "NONE",
    "content": "2023-01-01T01:01:01Z localhost proxy[12529]: 10.0.0.10:38440 http-in~ individual_servers/abc 217/0/0/1/218 HTTP_STATUS 200 284 - - --NN 5749/5745/0/1/0 0/0 {|||some.domain.com:443} {|} \"POST /path?param=foo HTTP/1.1\" ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2",
    "content2": "2023-01-01T01:01:01Z localhost proxy[12529]: 10.0.0.10:38440 http-in~ individual_servers/abc 217/0/0/1/218 HTTP_STATUS 200 284 - - --NN 5749/5745/0/1/0 0/0 {|||some.domain.com:443} {|} \"POST /path?param=foo HTTP/1.1\" ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2",
    "loglevel": "NONE",
    "host.name": "HOST-IG-12-34567",
    "timestamp": "2023-01-01T01:01:01.000000002Z",
    "event.type": "LOG",
    "log.source": "/var/spool/some.log",
    "dt.entity.host": "HOST-1234567890",
    "dt.source_entity": "PROCESS_GROUP_INSTANCE-ABCDEF1234567",
    "dt.security_context": "application-a",
    "dt.entity.process_group": "PROCESS_GROUP-ABCDEF1234567",
    "dt.entity.process_group_instance": "PROCESS_GROUP_INSTANCE-ABCDEF1234567"
  }
]

```

from this I want to extract the field&nbsp; **dt.entity.host** &nbsp;and then use it in second DQL.

Now let’s see the workflow part of it.

 ![Maheedhar_T_0-1728913189308.png](https://global.discourse-cdn.com/dynatrace/original/2X/5/50061dfce674ec524eecd45b6983e5ee1b8ae199.png)

This what our workflow looks like and let me break down stepwise.  
The first step is hosts\_cpu which is pretty standard just give the first DQL mentioned above.

 ![Maheedhar_T_1-1728913261338.png](https://global.discourse-cdn.com/dynatrace/original/3X/6/2/6207d3e26d2af284144f1aa97b8569e4e82f856a.png)

Now coming to the second step, this should be a loop task so go to the Options first and enable loop task. Now we want to loop this task over all the records that we get the output of step-1 (hosts\_cpu) so we configure something like this.

 ![Maheedhar_T_2-1728913368638.png](https://global.discourse-cdn.com/dynatrace/original/3X/9/6/96d0062dcb550eb07d898dd4129175952756fe05.png)

In the list you have to give something like

```markup
{{ result("hosts_cpu")["records"] }}

```

don’t worry about how I got this. Just under the list you’ll see option expand the list if you click that expand in the box and start typing records, you’ll get the options.

 ![Maheedhar_T_3-1728913766537.png](https://global.discourse-cdn.com/dynatrace/original/2X/6/62d4b086df3f1ea41e3b60652a4c31ca95a1965e.png)

Now the loop is in place, and we have to use that loop variable and then extract dt.entity.host from that variable.  
So the actual DQL in the second task becomes this.

 ![Maheedhar_T_4-1728913970700.png](https://global.discourse-cdn.com/dynatrace/original/2X/a/a28c7fd60badbdfe2ba5edec188c8c0552cac58c.png)

The DQL if expanded, looks like

```markup
fetch dt.entity.host
| filter id == "{{ _.item ['dt.entity.host'] | replace('"', "")}}"

```

Also, don’t bother about the replace part I had to use it because I was getting only one " like HOST-ABC12345" and I had to remove that " for the DQL to work properly.

Hope this helps.

Regards,  
[@Maheedhar\_T](https://community.dynatrace.com/user/76275)

---

<div class="post-metadata">

**Author:** ![nicemukesh](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@nicemukesh](https://community.dynatrace.com/u/nicemukesh)\
**Post date:** [October 15, 2024, 7:20am UTC](https://community.dynatrace.com/t/pass-the-result-of-dql-query-in-workflow/48233/10 "2024-10-15T07:20:52Z")

</div>

Thanks for your detailed explanation.. It is great help.

I will check it from my end and will keep posted on this

Mukesh

---

<div class="post-metadata">

**Author:** ![nicemukesh](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@nicemukesh](https://community.dynatrace.com/u/nicemukesh)\
**Post date:** [October 15, 2024, 12:36pm UTC](https://community.dynatrace.com/t/pass-the-result-of-dql-query-in-workflow/48233/11 "2024-10-15T12:36:12Z")

</div>

The main blocker in my DQL is, how to pass JSON outputs
