# Reformat of json data using Ansible

**URL:** <https://forum.ansible.com/t/reformat-of-json-data-using-ansible/35097>\
**Category:** Ansible Project\
**Created:** [May 3, 2021, 6:41am UTC](https://forum.ansible.com/t/reformat-of-json-data-using-ansible/35097 "2021-05-03T06:41:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amitchettri](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/amitchettri/32/404_2.png) [@amitchettri](https://forum.ansible.com/u/amitchettri)\
**Post date:** [May 3, 2021, 6:41am UTC](https://forum.ansible.com/t/reformat-of-json-data-using-ansible/35097/1 "2021-05-03T06:41:05Z")

</div>

Hello All,

Is it possible that we can reformat the json body completely using Ansible?  
As of now i have used the python script to read the json data and then do the reformatting of the json. But i am interest to know if the Ansible can do itself.

NOTE: can’t use the template as the contain would be dynamic. **So i need your opinion if this is even possible or not using Ansible or we need to use the script only for this?**

Example:  
**INPUT JSON:**  
{  
“access\_control”: ,  
“active”: true,  
“channel\_database\_cleanup”: {  
“enabled”: false  
},  
“client\_side\_hostkey”: {  
“plain\_hostkey”: {  
“dsa\_key”: null,  
“enabled”: true,  
“rsa\_key”: {  
“key”: “e5a58682-6189-4477-9415-67c1c9b20b0d”,  
“meta”: {  
“href”: “/api/configuration/private\_keys/e5a58682-6189-4477-9415-67c1c9b20b0d”  
}  
}  
},  
“x509\_hostkey”: {  
“enabled”: false  
}  
},  
“indexing”: {  
“enabled”: true,  
“policy”: {  
“key”: “-50000”,  
“meta”: {  
“href”: “/api/configuration/policies/indexing/-50000”  
}  
},  
“priority”: 3  
},  
“log\_audit\_trail\_downloads”: true,  
“name”: “Test”,  
“network”: {  
“clients”: [  
“x.x.x.x/x”  
],  
“ports”: [  
xx  
],  
“targets”: [  
“x.x.x.x/x”  
]  
},  
“override\_log\_level”: {  
“enabled”: false  
},  
“policies”: {  
“analytics\_policy”: {  
“key”: “437ba32f-e673-4070-b8da-6c40a6918598”,  
“meta”: {  
“href”: “/api/configuration/policies/analytics/437ba32f-e673-4070-b8da-6c40a6918598”  
}  
},  
“archive\_cleanup\_policy”: null,  
“audit\_policy”: {  
“key”: “78101850949e47437dd91d”,  
“meta”: {  
“href”: “/api/configuration/policies/audit\_policies/78101850949e47437dd91d”  
}  
}  
},  
“rate\_limit”: {  
“enabled”: flse  
}  
}

**OUTOUT JSON:**  
{  
“access\_control”: ,  
“active”: true,  
“channel\_database\_cleanup”: {  
“enabled”: false  
},  
“client\_side\_hostkey”: {  
“plain\_hostkey”: {  
“dsa\_key”: null,  
“enabled”: true,  
“rsa\_key”: {  
“key”: “e5a58682-6189-4477-9415-67c1c9b20b0d”,  
“meta”: {  
“href”: “/api/configuration/private\_keys/e5a58682-6189-4477-9415-67c1c9b20b0d”  
}  
}  
},  
“x509\_hostkey”: {  
“enabled”: false  
}  
},  
“indexing”: {  
“enabled”: true,  
“policy”: -50000  
},  
“priority”: 3  
},  
“log\_audit\_trail\_downloads”: true,  
“name”: “Test”,  
“network”: {  
“clients”: [  
“x.x.x.x/x”  
],  
“ports”: [  
xx  
],  
“targets”: [  
“x.x.x.x/x”  
]  
},  
“override\_log\_level”: {  
“enabled”: false  
},  
“policies”: {  
“analytics\_policy”: “437ba32f-e673-4070-b8da-6c40a691859”,  
“archive\_cleanup\_policy”: null,  
“audit\_policy”: “78101850949e47437dd91d”  
},  
“rate\_limit”: {  
“enabled”: flse  
}  
}

Regards  
Amit

---

<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 3, 2021, 7:01am UTC](https://forum.ansible.com/t/reformat-of-json-data-using-ansible/35097/2 "2021-05-03T07:01:31Z")

</div>

Instead of us doing an eye ball diff of these complex structures, please explain what you mean exactly by reformatting.

---

<div class="post-metadata">

**Author:** ![amitchettri](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/amitchettri/32/404_2.png) [@amitchettri](https://forum.ansible.com/u/amitchettri)\
**Post date:** [May 3, 2021, 7:13am UTC](https://forum.ansible.com/t/reformat-of-json-data-using-ansible/35097/3 "2021-05-03T07:13:14Z")

</div>

Hi Dick,

Input json is my json file contain that i need to format it as per the output json as show in the previous mail thread,

The formatted json is required by the API call hence need to remove few key out of it.

actually json( input json) have a key in below format where we we need to remove “key and meta” keys and put the key value next to the policy name

**example:**  
**actual json will content key value as below**  
“authentication\_policy”: {  
“key”: “1895203635707e3340262f”,  
“meta”: {  
“href”: “/api/configuration/ssh/authentication\_policies/1895203635707e3340262f”  
}  
}

**changes that need to take place is as below**

1. remove the key and move the key value next to policy name
2. remove meta block  
“authentication\_policy”: “1895203635707e3340262f”,

NOTE: I have been able to do this task using python, but i looking forward to know that ansible can do this or not.

---

<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 3, 2021, 8:34am UTC](https://forum.ansible.com/t/reformat-of-json-data-using-ansible/35097/4 "2021-05-03T08:34:19Z")

</div>

Ah ok, so this is basically the same question as the other message you posted.  
You should be able to achieve this with the combine filter: [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:** ![amitchettri](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/amitchettri/32/404_2.png) [@amitchettri](https://forum.ansible.com/u/amitchettri)\
**Post date:** [May 4, 2021, 12:17pm UTC](https://forum.ansible.com/t/reformat-of-json-data-using-ansible/35097/5 "2021-05-04T12:17:01Z")

</div>

Using combine it basically overwrite the content, but i dont wanna do that

Regards  
Amit

---

<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 4, 2021, 1:10pm UTC](https://forum.ansible.com/t/reformat-of-json-data-using-ansible/35097/6 "2021-05-04T13:10:55Z")

</div>

Actually, I think you do want that.  
Your original variable is "incorrectly formatted", i.e. it's structure  
is not what you want.  
Use that original variable to construct another variable, with the  
correct "formatting".  
Then use that for whatever you want to do next.

Dick

---

<div class="post-metadata">

**Author:** ![amitchettri](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/amitchettri/32/404_2.png) [@amitchettri](https://forum.ansible.com/u/amitchettri)\
**Post date:** [May 4, 2021, 8:09pm UTC](https://forum.ansible.com/t/reformat-of-json-data-using-ansible/35097/7 "2021-05-04T20:09:54Z")

</div>

Hi Dick,

As of now I am able to make the changes using python. Was only looking for a way to get this done using the ansible, which from my side I felt, it’s hard to get it done.

If anyone wants to take this as a use-case example then please consider it. may be it will be useful for someone

Regards  
Amit
