# Removing a deep nested element from dict (json structure)

**URL:** <https://forum.ansible.com/t/removing-a-deep-nested-element-from-dict-json-structure/33014>\
**Category:** Ansible Project\
**Created:** [May 24, 2020, 1:10pm UTC](https://forum.ansible.com/t/removing-a-deep-nested-element-from-dict-json-structure/33014 "2020-05-24T13:10:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastien\_Morissette](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@sebastien\_Morissette](https://forum.ansible.com/u/sebastien_Morissette)\
**Post date:** [May 24, 2020, 1:10pm UTC](https://forum.ansible.com/t/removing-a-deep-nested-element-from-dict-json-structure/33014/1 "2020-05-24T13:10:52Z")

</div>

Hi,

i’ve been at this for several hours and i’m going in circles so I would like to see if someone can help me get it right.

Given a dict structure which is a json array, I am looking to modify only the deeply nested users[\*].perms object to remove the “delete” list item…

---

<div class="post-metadata">

**Author:** ![sebastien\_Morissette](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@sebastien\_Morissette](https://forum.ansible.com/u/sebastien_Morissette)\
**Post date:** [May 24, 2020, 1:13pm UTC](https://forum.ansible.com/t/removing-a-deep-nested-element-from-dict-json-structure/33014/2 "2020-05-24T13:13:23Z")

</div>

> The result I am looking for is the following :

{  
“id”: “209”,  
“ownerUserId”: “2”,  
“resource”: “[https://cloudcenter.cp.intactfc.com/v1/acls?id=209&resourceName=ACI\_EXTENSION](https://cloudcenter.cp.intactfc.com/v1/acls?id=209&resourceName=ACI_EXTENSION)”,  
“resourceName”: “ACI\_EXTENSION”,  
“role”: null,  
“tenantAndSubtenantUsers”: [  
{  
“id”: “1”,  
“perms”: [  
“read”  
]  
},  
{  
“id”: “108”,  
“perms”: [  
“read”  
]  
}  
],  
“tenantUsers”: [  
{  
“id”: “1”,  
“name”: “[cloudcenter.cp.intactfc.com](http://cloudcenter.cp.intactfc.com/)”,  
“parentTenantId”: null,  
“perms”: [  
“read”,  
“access”  
],  
“resource”: “[https://cloudcenter.cp.intactfc.com/v1/acls/1?id=209&resourceName=ACI\_EXTENSION](https://cloudcenter.cp.intactfc.com/v1/acls/1?id=209&resourceName=ACI_EXTENSION)”,  
“shortName”: “ifc-cp”,  
“userId”: “2”  
}  
],  
“userGroups”: ,  
“users”: [  
{  
“emailAddr”: “[admin@cliqrtech.com](mailto:admin@cliqrtech.com)”,  
“enabled”: true,  
“firstName”: “Cliqr”,  
“id”: “2”,  
“lastName”: “Admin”,  
**“perms”: [**  
**“administration”,**  
**“write”,**  
**“read”,**  
**“access”**

**],**  
“resource”: “[https://cloudcenter.cp.intactfc.com/v1/acls/2?id=209&resourceName=ACI\_EXTENSION](https://cloudcenter.cp.intactfc.com/v1/acls/2?id=209&resourceName=ACI_EXTENSION)”,  
“tenantId”: “1”,  
“type”: “TENANT”,  
“username”: “cliqradmin”  
},  
{  
“emailAddr”: “[admin2@cliqrtech.com](mailto:admin2@cliqrtech.com)”,  
“enabled”: true,  
“firstName”: “Cliqr”,  
“id”: “3”,  
“lastName”: “Admin2”,  
**“perms”: [**  
**“write”,**  
**“read”,**  
**],**

“resource”: “[https://cloudcenter.cp.intactfc.com/v1/acls/2?id=209&resourceName=ACI\_EXTENSION](https://cloudcenter.cp.intactfc.com/v1/acls/2?id=209&resourceName=ACI_EXTENSION)”,  
“tenantId”: “1”,  
“type”: “TENANT”,  
“username”: “cliqradmin2”  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![sebastien\_Morissette](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@sebastien\_Morissette](https://forum.ansible.com/u/sebastien_Morissette)\
**Post date:** [May 24, 2020, 2:44pm UTC](https://forum.ansible.com/t/removing-a-deep-nested-element-from-dict-json-structure/33014/3 "2020-05-24T14:44:27Z")

</div>

> tested the json in jq, had errors in it… here is the updated json

{  
“id”: “209”,  
“ownerUserId”: “2”,  
“resource”: “[https://abc.inet/v1/acls?id=209&resourceName=ACI\_EXTENSION](https://abc.inet/v1/acls?id=209&resourceName=ACI_EXTENSION)”,  
“resourceName”: “ACI\_EXTENSION”,  
“role”: null,  
“tenantAndSubtenantUsers”: [  
{  
“id”: “1”,  
“perms”: [  
“read”  
]  
},  
{  
“id”: “108”,  
“perms”: [  
“read”  
]  
}  
],  
“tenantUsers”: [  
{  
“id”: “1”,  
“name”: “abc.inet”,  
“parentTenantId”: null,  
“perms”: [  
“read”,  
“access”  
],  
“resource”: “[https://abc.inet/v1/acls/1?id=209&resourceName=ACI\_EXTENSION](https://abc.inet/v1/acls/1?id=209&resourceName=ACI_EXTENSION)”,  
“shortName”: “cp”,  
“userId”: “2”  
}  
],  
“userGroups”: ,  
“users”: [  
{  
“emailAddr”: “[admin@cliqrtech.com](mailto:admin@cliqrtech.com)”,  
“enabled”: true,  
“firstName”: “Cliqr”,  
“id”: “2”,  
“lastName”: “Admin”,  
“perms”: [  
“administration”,  
“write”,  
“read”,  
“delete”,  
“access”  
],  
“resource”: “abc.inet/v1/acls/2?id=209&resourceName=ACI\_EXTENSION”,  
“tenantId”: “1”,  
“type”: “TENANT”,  
“username”: “cliqradmin”  
},  
{  
“emailAddr”: “[admin2@cliqrtech.com](mailto:admin2@cliqrtech.com)”,  
“enabled”: true,  
“firstName”: “Cliqr”,  
“id”: “3”,  
“lastName”: “Admin2”,  
“perms”: [  
“write”,  
“read”,  
“delete”  
],  
“resource”: “abc.inet/v1/acls/2?id=209&resourceName=ACI\_EXTENSION”,  
“tenantId”: “1”,  
“type”: “TENANT”,  
“username”: “cliqradmin2”  
}  
]

}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 24, 2020, 4:56pm UTC](https://forum.ansible.com/t/removing-a-deep-nested-element-from-dict-json-structure/33014/4 "2020-05-24T16:56:53Z")

</div>

I would combine this dict with another Dict that what you want.

See  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#combining-hashes-dictionaries](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#combining-hashes-dictionaries)

---

<div class="post-metadata">

**Author:** ![sebastien\_Morissette](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@sebastien\_Morissette](https://forum.ansible.com/u/sebastien_Morissette)\
**Post date:** [May 24, 2020, 5:04pm UTC](https://forum.ansible.com/t/removing-a-deep-nested-element-from-dict-json-structure/33014/5 "2020-05-24T17:04:46Z")

</div>

Hi Dick,

i’ve been trying, but the issue is that I am receiving this structre back from a call to an api / rest using uri module,

then I need to post back the updated structure but after many hours i’m not getting the combine to work as it should…

i’m probably turning arround the solution just not figuring it out…

each user has existing perms… then i need only to remove the “delete” from each perms array, individually for each user without removing the rest of the entries. (delete is not a valid option when posting back to the API… it’S a bug on the vendor side i have to work arround…)

i’m still at it… trying to get it to work… I started looking into calling shell with JQ to try and achieve it…

---

<div class="post-metadata">

**Author:** ![sebastien\_Morissette](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@sebastien\_Morissette](https://forum.ansible.com/u/sebastien_Morissette)\
**Post date:** [May 24, 2020, 5:07pm UTC](https://forum.ansible.com/t/removing-a-deep-nested-element-from-dict-json-structure/33014/6 "2020-05-24T17:07:32Z")

</div>

Also the combine examples seem to allways refer to key / value pairs… in the perms list i only have values… the keys are not present …

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 24, 2020, 6:28pm UTC](https://forum.ansible.com/t/removing-a-deep-nested-element-from-dict-json-structure/33014/7 "2020-05-24T18:28:16Z")

</div>

This should work:

&nbsp;&nbsp;tasks:

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filtered\_users: "{{ filtered\_users|default() | union( [  
item | combine( {'perms': item.perms|difference(['delete']) } )] ) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_body|json\_query('users') }}"

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_body: "{{ my\_body | combine({'users': filtered\_users}) }}"

&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=new\_body

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\>  
&nbsp;&nbsp;new\_body:  
&nbsp;&nbsp;&nbsp;&nbsp;id: '209'  
&nbsp;&nbsp;&nbsp;&nbsp;ownerUserId: '2'  
&nbsp;&nbsp;&nbsp;&nbsp;resource: [https://abc.inet/v1/acls?id=209&resourceName=ACI\_EXTENSION](https://abc.inet/v1/acls?id=209&resourceName=ACI_EXTENSION)  
&nbsp;&nbsp;&nbsp;&nbsp;resourceName: ACI\_EXTENSION  
&nbsp;&nbsp;&nbsp;&nbsp;role: null  
&nbsp;&nbsp;&nbsp;&nbsp;tenantAndSubtenantUsers:  
&nbsp;&nbsp;&nbsp;&nbsp;- id: '1'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perms:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- read  
&nbsp;&nbsp;&nbsp;&nbsp;- id: '108'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perms:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- read  
&nbsp;&nbsp;&nbsp;&nbsp;tenantUsers:  
&nbsp;&nbsp;&nbsp;&nbsp;- id: '1'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: abc.inet  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parentTenantId: null  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perms:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- read  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- access  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resource: [https://abc.inet/v1/acls/1?id=209&resourceName=ACI\_EXTENSION](https://abc.inet/v1/acls/1?id=209&resourceName=ACI_EXTENSION)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shortName: cp  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userId: '2'  
&nbsp;&nbsp;&nbsp;&nbsp;userGroups:   
&nbsp;&nbsp;&nbsp;&nbsp;users:  
&nbsp;&nbsp;&nbsp;&nbsp;- emailAddr: admin@cliqrtech.com  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enabled: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;firstName: Cliqr  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id: '2'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastName: Admin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perms:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- administration  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- write  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- read  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- access  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resource: abc.inet/v1/acls/2?id=209&resourceName=ACI\_EXTENSION  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tenantId: '1'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type: TENANT  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: cliqradmin  
&nbsp;&nbsp;&nbsp;&nbsp;- emailAddr: admin2@cliqrtech.com  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enabled: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;firstName: Cliqr  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id: '3'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastName: Admin2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perms:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- write  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- read  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resource: abc.inet/v1/acls/2?id=209&resourceName=ACI\_EXTENSION  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tenantId: '1'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type: TENANT  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: cliqradmin2

---

<div class="post-metadata">

**Author:** ![sebastien\_Morissette](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@sebastien\_Morissette](https://forum.ansible.com/u/sebastien_Morissette)\
**Post date:** [May 24, 2020, 7:05pm UTC](https://forum.ansible.com/t/removing-a-deep-nested-element-from-dict-json-structure/33014/8 "2020-05-24T19:05:15Z")

</div>

wow! it works perfectly!!!

I was going about it the wrong way… I was decomposing the whole structure to pieces, to update those and then reconstruct… i was ending up getting into nested loop issues because I needed to iterate over both the number of perms and the number of items in the array…

anyways… suffice it to say you solution is much more concise and elegant!

thanks so much!
