# template

**URL:** <https://forum.ansible.com/t/template/26128>\
**Category:** Ansible Project\
**Created:** [November 24, 2017, 4:18pm UTC](https://forum.ansible.com/t/template/26128 "2017-11-24T16:18:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Iuga](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@Christian\_Iuga](https://forum.ansible.com/u/Christian_Iuga)\
**Post date:** [November 24, 2017, 4:18pm UTC](https://forum.ansible.com/t/template/26128/1 "2017-11-24T16:18:26Z")

</div>

Hi,

I’m trying to build custom json file like this :

{  
“client”: {  
“subscriptions”: {{ sensu\_client\_subscriptions | to\_nice\_json }}  
{% if sensu\_autoderegister %}  
,“deregister”: true,  
“deregistration”: {  
“handler”: “deregister\_client”  
}  
{%endif%}  
{% if sensu\_custom\_value %}  
,{{ sensu\_custom\_value| to\_nice\_json }}  
{%endif%}  
}  
}

with variable :  
sensu\_custom\_value:  
disk:  
warning: “85”  
critical: “90”  
postfix:  
warning: “10”  
critical: “20”

My issue is with {{ sensu\_custom\_value| to\_nice\_json }}  
it’s produce unwanted (for me) extra bracket :  
,{  
“disk”: {  
“critical”: “90”,  
“warning”: “85”  
},  
“postfix”: {  
“critical”: “20”,  
“warning”: “10”  
}  
}

i whould like to have, instead :

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 24, 2017, 4:47pm UTC](https://forum.ansible.com/t/template/26128/2 "2017-11-24T16:47:43Z")

</div>

> Hi,
> 
> I'm trying to build custom json file like this :
> 
> {  
> &nbsp;&nbsp;"client": {  
> &nbsp;&nbsp;&nbsp;&nbsp;"subscriptions": {{ sensu\_client\_subscriptions | to\_nice\_json }}  
> {% if sensu\_autoderegister %}  
> &nbsp;&nbsp;&nbsp;&nbsp;,"deregister": true,  
> &nbsp;&nbsp;&nbsp;&nbsp;"deregistration": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"handler": "deregister\_client"  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> {%endif%}  
> {% if sensu\_custom\_value %}  
> &nbsp;&nbsp;&nbsp;,{{ sensu\_custom\_value| to\_nice\_json }}  
> {%endif%}  
> &nbsp;&nbsp;}  
> }
> 
> with variable :  
> sensu\_custom\_value:  
> &nbsp;&nbsp;disk:  
> &nbsp;&nbsp;&nbsp;&nbsp;warning: "85"  
> &nbsp;&nbsp;&nbsp;&nbsp;critical: "90"  
> &nbsp;&nbsp;postfix:  
> &nbsp;&nbsp;&nbsp;&nbsp;warning: "10"  
> &nbsp;&nbsp;&nbsp;&nbsp;critical: "20"
> 
> My issue is with {{ sensu\_custom\_value| to\_nice\_json }}  
> it's produce unwanted (for me) extra bracket :

That because the correct representation of sensu\_custion\_value in json need to have those brackets.

> &nbsp;&nbsp;&nbsp;,{  
> &nbsp;&nbsp;&nbsp;&nbsp;"disk": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"critical": "90",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"warning": "85"  
> &nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;"postfix": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"critical": "20",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"warning": "10"  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> }
> 
> i whould like to have, instead :  
> &nbsp;&nbsp;&nbsp;,  
> &nbsp;&nbsp;&nbsp;&nbsp;"disk": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"critical": "90",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"warning": "85"  
> &nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;"postfix": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"critical": "20",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"warning": "10"  
> &nbsp;&nbsp;&nbsp;&nbsp;}
> 
> any idea ?

You could do something like this, iterate over the keys and print an output.

{% for key in sensu\_custom\_value %},\n"{{ key }}": {{ sensu\_custom\_value[key] | to\_nice\_json }}{%endfor%}
