# Ansible Split JSON Return

**URL:** <https://forum.ansible.com/t/ansible-split-json-return/32392>\
**Category:** Ansible Project\
**Created:** [March 5, 2020, 2:52pm UTC](https://forum.ansible.com/t/ansible-split-json-return/32392 "2020-03-05T14:52:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [March 5, 2020, 2:52pm UTC](https://forum.ansible.com/t/ansible-split-json-return/32392/1 "2020-03-05T14:52:52Z")

</div>

Have the Following Playbook Which Returns the following

ok: [localhost] =\> {  
“mdb.json”: [  
{  
“mac-address”: “xx:xx:xx:xx:xx”  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![Cade\_Lambert](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cade\_Lambert](https://forum.ansible.com/u/Cade_Lambert)\
**Post date:** [March 5, 2020, 3:28pm UTC](https://forum.ansible.com/t/ansible-split-json-return/32392/2 "2020-03-05T15:28:54Z")

</div>

Something like this would work I believe:

“{{ mdb.json.split(‘:’)[1:6] | join(‘:’) }}”

Also, could you not just reference it directly by using “{{ mdb.json.mac-address }}”?

---

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [March 5, 2020, 3:34pm UTC](https://forum.ansible.com/t/ansible-split-json-return/32392/3 "2020-03-05T15:34:40Z")

</div>

When i do Both

“{{ mdb.json.mac-address }}”

“{{ mdb.json.split(‘:’)[1:6] | join(‘:’) }}”

It returns

fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘list object’ has no attribute ‘mac’\n\nThe error appears to be in ‘/srv/Ansible/mdb.yaml’: line 8, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: mdb\n - debug:\n ^ here\n”}

fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘list object’ has no attribute ‘split’\n\nThe error appears to be in ‘/srv/Ansible/mdb.yaml’: line 8, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: mdb\n - debug:\n ^ here\n”}

---

<div class="post-metadata">

**Author:** ![Cade\_Lambert](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cade\_Lambert](https://forum.ansible.com/u/Cade_Lambert)\
**Post date:** [March 5, 2020, 3:39pm UTC](https://forum.ansible.com/t/ansible-split-json-return/32392/4 "2020-03-05T15:39:34Z")

</div>

What’s your plabook look like? You’ll probably have to loop through your JSON results as it looks like it’s a list.

---

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [March 5, 2020, 3:45pm UTC](https://forum.ansible.com/t/ansible-split-json-return/32392/5 "2020-03-05T15:45:03Z")

</div>



---

<div class="post-metadata">

**Author:** ![Cade\_Lambert](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cade\_Lambert](https://forum.ansible.com/u/Cade_Lambert)\
**Post date:** [March 5, 2020, 3:55pm UTC](https://forum.ansible.com/t/ansible-split-json-return/32392/6 "2020-03-05T15:55:36Z")

</div>

Ok, try this then:

- debug:  
msg: “{{ item.mac-address }}”  
loop: “{{ mdb.json }}”

---

<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:** [March 5, 2020, 8:47pm UTC](https://forum.ansible.com/t/ansible-split-json-return/32392/7 "2020-03-05T20:47:55Z")

</div>

> Have the Following Playbook Which Returns the following
> 
> ok: [localhost] =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;"mdb.json": [

The [ is telling us that mdb.json is a list, the fist element in a list i 0.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mac-address": "xx:xx:xx:xx:xx"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;]  
> }

\<snip /\>

> &nbsp;&nbsp;- debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ mdb.json.split(':') }}"
> 
> What i would like to Return is just
> 
> "msg": "xx:xx:xx:xx:xx"

That would be {{ mdb.json.0['mac-address'] }}

because of the dash in mac-address you need to use notation and not the dot notation on that one.

---

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [March 6, 2020, 1:38pm UTC](https://forum.ansible.com/t/ansible-split-json-return/32392/8 "2020-03-06T13:38:03Z")

</div>

Thanks Kai,

that worked i was also able to Split(‘-’)[0] and that only outputted the xxx:xxx:xxx:xx just what i was looking for.
