# Filter API result by nested field

**URL:** <https://forum.ansible.com/t/filter-api-result-by-nested-field/6014>\
**Category:** Get Help\
**Tags:** api, awx\
**Created:** [May 27, 2024, 6:42am UTC](https://forum.ansible.com/t/filter-api-result-by-nested-field/6014 "2024-05-27T06:42:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@matthias1](https://forum.ansible.com/u/matthias1)\
**Post date:** [May 27, 2024, 6:42am UTC](https://forum.ansible.com/t/filter-api-result-by-nested-field/6014/1 "2024-05-27T06:42:44Z")

</div>

I’m downloading a list of approvals: /api/v2/workflow\_approvals/  
I want to filter by the user who launched the request. If I look at the json it’s in this field:  
results/launched\_by/name

I can’t figure out how to filter on this.

Any tips?

---

<div class="post-metadata">

**Author:** ![matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@matthias1](https://forum.ansible.com/u/matthias1)\
**Post date:** [May 29, 2024, 6:58am UTC](https://forum.ansible.com/t/filter-api-result-by-nested-field/6014/2 "2024-05-29T06:58:58Z")

</div>

Is there more information needed from my side?

Here’s a screenshot of a part of the response. I want to narrow down on the name (here donald\_duck).

 ![Screenshot 2024-05-29 at 08.56.28](https://us1.discourse-cdn.com/ansible/original/2X/8/8872171d641525601e96944a97c17e8579d1f6b7.png)

---

<div class="post-metadata">

**Author:** ![motorbass](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@motorbass](https://forum.ansible.com/u/motorbass)\
**Post date:** [May 29, 2024, 9:54am UTC](https://forum.ansible.com/t/filter-api-result-by-nested-field/6014/3 "2024-05-29T09:54:38Z")

</div>

Hi !  
If i well understood you basically need to parse a JSON data in a file right ?

If yes you can basically use something like read JSON file then parse it , have a look to :

> **[Ansible read JSON file - JSON file Parsing | DevOps Junction](https://www.middlewareinventory.com/blog/ansible-playbook-read-json-file/)**
>
> Ansible JSON file, Ansible read JSON file and parse the JSON Data with Ansible. Parsing JSON with ansible example. Ansible JSON\_query and Ansible JSON example. In this article we are covering in detail how to read JSON data into Ansible playbook and...

AND

[https://docs.ansible.com/ansible/latest/collections/community/general/docsite/filter\_guide\_selecting\_json\_data.html](https://docs.ansible.com/ansible/latest/collections/community/general/docsite/filter_guide_selecting_json_data.html)

---

<div class="post-metadata">

**Author:** ![matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@matthias1](https://forum.ansible.com/u/matthias1)\
**Post date:** [May 29, 2024, 10:41am UTC](https://forum.ansible.com/t/filter-api-result-by-nested-field/6014/4 "2024-05-29T10:41:25Z")

</div>

No sorry. I don’t want to parse a JSON file.  
I want to do an API access to the _/api/v2/workflow\_approvals/_ endpoint.  
One could specify filters at the end of the URL to narrow down the results.  
The results consist of a JSON file. There are top level fields and nested fields.  
E.g. _status_ is a top level field and I could filter using something like _/api/v2/workflow\_approvals/?status=pending_.  
But I want to filter for a certain user. But the user name is a nested field (see screenshot above). So I don’t know how to specify it in the API query.

---

<div class="post-metadata">

**Author:** ![motorbass](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@motorbass](https://forum.ansible.com/u/motorbass)\
**Post date:** [May 29, 2024, 11:50am UTC](https://forum.ansible.com/t/filter-api-result-by-nested-field/6014/5 "2024-05-29T11:50:59Z")

</div>

Ok sorry me for the previous misunderstood 🙂  
I had a quick look at the [API reference](https://docs.ansible.com/ansible-tower/latest/html/towerapi/api_ref.html#/Workflow_Approvals/Workflow_Approvals_workflow_approvals_list) but it seems there’s no native way to do it.

Depends on what you’re trying to do, why don’t you store the API call result you got somewhere in a variable, and then parse it as a json file/result and then, launch api calls on condition for instance ?

I mean as API doesn’t seems to have the option, so store data you need and filtering then in a second time

OR

use the `?order_by=name` but i assume it’s workflow’s name, not user.

---

<div class="post-metadata">

**Author:** ![matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@matthias1](https://forum.ansible.com/u/matthias1)\
**Post date:** [May 29, 2024, 12:54pm UTC](https://forum.ansible.com/t/filter-api-result-by-nested-field/6014/6 "2024-05-29T12:54:58Z")

</div>

Hi, thanks for coming back. I just wasn’t sure if it’s possible or not. At the moment I’m downloading the result and parsing it offline. Of course it would be better if I could only get the result I need by filtering it already in the API call.  
So it’s not possible, right?

---

<div class="post-metadata">

**Author:** ![motorbass](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@motorbass](https://forum.ansible.com/u/motorbass)\
**Post date:** [May 29, 2024, 12:56pm UTC](https://forum.ansible.com/t/filter-api-result-by-nested-field/6014/7 "2024-05-29T12:56:41Z")

</div>

According to the documentation, i think it’s not possible yet, unfortunately.

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [May 29, 2024, 7:00pm UTC](https://forum.ansible.com/t/filter-api-result-by-nested-field/6014/8 "2024-05-29T19:00:27Z")

</div>

On the contrary, this is possible.

If you browse to `/api/v2/workflow_approvals/` on your instance, and click the “options” button, it will show you the valid `search_fields` and `related_search_fields`. You can search nested values of valid objects using double underscores.

So for example, you can use this to filter all approved/denied workflows by a given username:  
`/api/v2/workflow_approvals/?approved_or_denied_by__username=foo_user`  
or by id `/api/v2/workflow_approvals/?approved_or_denied_by__id=2`

Similarly, if you’re wanting pending approvals _launched_ by a particular user:  
`/api/v2/workflow_approvals/?status=pending&created_by__username=foo_user`

---

<div class="post-metadata">

**Author:** ![matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@matthias1](https://forum.ansible.com/u/matthias1)\
**Post date:** [May 31, 2024, 5:42am UTC](https://forum.ansible.com/t/filter-api-result-by-nested-field/6014/10 "2024-05-31T05:42:09Z")

</div>

Hi, thank you very much!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 30, 2024, 5:42am UTC](https://forum.ansible.com/t/filter-api-result-by-nested-field/6014/11 "2024-06-30T05:42:49Z")

</div>

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