# How to split a json key containing a colon into 2 keys

**URL:** <https://forum.ansible.com/t/how-to-split-a-json-key-containing-a-colon-into-2-keys/32175>\
**Category:** Ansible Project\
**Created:** [February 7, 2020, 4:14pm UTC](https://forum.ansible.com/t/how-to-split-a-json-key-containing-a-colon-into-2-keys/32175 "2020-02-07T16:14:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [February 7, 2020, 4:14pm UTC](https://forum.ansible.com/t/how-to-split-a-json-key-containing-a-colon-into-2-keys/32175/1 "2020-02-07T16:14:56Z")

</div>

I receive some json data which **sometimes** start with the first key within “json\_data” variable containing a ‘:’, such as:

`

“json\_data”: {  
“key1:key2”: {  
“key3”: [  
“value31”,  
“value32”,  
“…”  
]  
}  
}

`

The goal is to split “key1:key2” into 2 keys so that we end up with:

`

“json\_data”: {  
“key1”: {  
“key2”: {  
“key3”: [  
“value31”,  
“value32”,  
“…”  
]  
}  
}  
}

`

The key names are completely variable so I also have no idea about how to implement the play condition so that this transformation is done only when the first key within “json\_data” contains a single colon.

I am aware that a colon should not be part of any key, but I have no control over that.

Any suggestion?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 7, 2020, 6:12pm UTC](https://forum.ansible.com/t/how-to-split-a-json-key-containing-a-colon-into-2-keys/32175/2 "2020-02-07T18:12:21Z")

</div>

Try this

&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_data1: "{{ json\_data1|default({})|combine(my\_key) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_keys: "{{ item.key.split(':') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_keys\_length: "{{ my\_keys|length == 1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_key: "{{ my\_keys\_length|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ternary({my\_keys[0]: item.value},  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{my\_keys[0]: {my\_keys[1:]|join(':')|default('NA'):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.value}}) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ json\_data|dict2items }}"

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [February 11, 2020, 5:39pm UTC](https://forum.ansible.com/t/how-to-split-a-json-key-containing-a-colon-into-2-keys/32175/3 "2020-02-11T17:39:06Z")

</div>

I’m impressed. It works perfectly. Only the first key is split as expected…
