# Parsing json files with ansible

**URL:** <https://forum.ansible.com/t/parsing-json-files-with-ansible/35198>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [May 19, 2021, 11:17pm UTC](https://forum.ansible.com/t/parsing-json-files-with-ansible/35198 "2021-05-19T23:17:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rodrigues](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@Rodrigues](https://forum.ansible.com/u/Rodrigues)\
**Post date:** [May 19, 2021, 11:17pm UTC](https://forum.ansible.com/t/parsing-json-files-with-ansible/35198/1 "2021-05-19T23:17:17Z")

</div>

I’m trying to parse the json output of a VMware DVSwitch which i’ve extracted using Ansible module community.vmware.vmware\_dvs\_portgroup\_info

using this role, however i can’t seem to get to the portgroups, can only display the full json. What i’m i missing?

_ **- hosts: 127.0.0.1** _  
_ **connection: local** _  
_ **become: yes** _  
_ **vars\_files:** _  
_ **- answerfile\_test.yml** _  
_ **- …/cred.yaml** _  
_ **vars:** _  
_**dvs: “{{ lookup(‘file’, ‘result.json’) }}” tasks: - name: Simple debug.**_  
_ **debug:** _  
_**msg: “{{ dvs.dvs\_portgroup\_info["DVS-Dev-Ork2"] }}”**_  
_ **tags:** _  
_ **- G2** _

Json output here:

{“changed”: false, “dvs\_portgroup\_info”: {“DVS-Dev-Ork2”: [{“portgroup\_name”: “Test - 192.168.20.0\_24 - VMotion - 1”, “num\_ports”: 8, “dvswitch\_name”: “DVS-Dev-Ork2”, “description”: null, “type”: “earlyBinding”, “teaming\_policy”: {“policy”: “loadbalance\_ip”, “inbound\_policy”: true, “notify\_switches”: true, “rolling\_order”: false}, “port\_policy”: {“block\_override”: true, “ipfix\_override”: false, “live\_port\_move”: false, “network\_rp\_override”: false, “port\_config\_reset\_at\_disconnect”: true, “security\_override”: false, “shaping\_override”: false, “traffic\_filter\_override”: false, “uplink\_teaming\_override”: false, “vendor\_config\_override”: false, “vlan\_override”: false}, “network\_policy”: {“forged\_transmits”: false, “promiscuous”: false, “mac\_changes”: false}, “vlan\_info”: {“trunk”: false, “pvlan”: false, “vlan\_id”: “20”}, “key”: “dvportgroup-14”}, {“portgroup\_name”: “Test - 192.168.60.0\_24 - Management”, “num\_ports”: 15, “dvswitch\_name”: “DVS-Dev-Ork2”, “description”: null, “type”: “earlyBinding”, “teaming\_policy”: {“policy”: “loadbalance\_ip”, “inbound\_policy”: true, “notify\_switches”: true, “rolling\_order”: false}, “port\_policy”: {“block\_override”: true, “ipfix\_override”: false, “live\_port\_move”: false, “network\_rp\_override”: false, “port\_config\_reset\_at\_disconnect”: true, “security\_override”: false, “shaping\_override”: false, “traffic\_filter\_override”: false, “uplink\_teaming\_override”: false, “vendor\_config\_override”: false, “vlan\_override”: false}, “network\_policy”: {“forged\_transmits”: false, “promiscuous”: false, “mac\_changes”: false}, “vlan\_info”: {“trunk”: false, “pvlan”: false, “vlan\_id”: “0”}, “key”: “dvportgroup-13”}, {“portgroup\_name”: “Test - 192.168.30.0\_24 - VSAN”, “num\_ports”: 8, “dvswitch\_name”: “DVS-Dev-Ork2”, “description”: null, “type”: “earlyBinding”, “teaming\_policy”: {“policy”: “loadbalance\_ip”, “inbound\_policy”: true, “notify\_switches”: true, “rolling\_order”: false}, “port\_policy”: {“block\_override”: true, “ipfix\_override”: false, “live\_port\_move”: false, “network\_rp\_override”: false, “port\_config\_reset\_at\_disconnect”: true, “security\_override”: false, “shaping\_override”: false, “traffic\_filter\_override”: false, “uplink\_teaming\_override”: false, “vendor\_config\_override”: false, “vlan\_override”: false}, “network\_policy”: {“forged\_transmits”: false, “promiscuous”: false, “mac\_changes”: false}, “vlan\_info”: {“trunk”: false, “pvlan”: false, “vlan\_id”: “25”}, “key”: “dvportgroup-15”}, {“portgroup\_name”: “DVS-Dev-Ork2-DVUplinks-11”, “num\_ports”: 18, “dvswitch\_name”: “DVS-Dev-Ork2”, “description”: null, “type”: “earlyBinding”, “teaming\_policy”: {“policy”: “loadbalance\_srcid”, “inbound\_policy”: true, “notify\_switches”: true, “rolling\_order”: false}, “port\_policy”: {“block\_override”: true, “ipfix\_override”: false, “live\_port\_move”: false, “network\_rp\_override”: false, “port\_config\_reset\_at\_disconnect”: true, “security\_override”: false, “shaping\_override”: false, “traffic\_filter\_override”: false, “uplink\_teaming\_override”: false, “vendor\_config\_override”: false, “vlan\_override”: false}, “network\_policy”: {“forged\_transmits”: true, “promiscuous”: false, “mac\_changes”: false}, “vlan\_info”: {“trunk”: true, “pvlan”: false, “vlan\_id”: [“0-4094”]}, “key”: “dvportgroup-12”}]}, “failed”: false}

---

<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 20, 2021, 4:10am UTC](https://forum.ansible.com/t/parsing-json-files-with-ansible/35198/2 "2021-05-20T04:10:47Z")

</div>

> I’m trying to parse the json output of a VMware DVSwitch which i’ve extracted using Ansible module community.vmware.vmware\_dvs\_portgroup\_info
> 
> using this role, however i can’t seem to get to the portgroups, can only display the full json. What i’m i missing?
> 
> _ **- hosts: 127.0.0.1** _  
> _ **connection: local** _  
> _ **become: yes** _  
> _ **vars\_files:** _  
> _ **- answerfile\_test.yml** _  
> _ **- …/cred.yaml** _  
> _ **vars:** _  
> _**dvs: “{{ lookup(‘file’, ‘result.json’) }}” tasks: - name: Simple debug.**_  
> _ **debug:** _  
> _**msg: “{{ dvs.dvs\_portgroup\_info["DVS-Dev-Ork2"] }}”**_  
> _ **tags:** _  
> _ **- G2** _
> 
> Json output here:

Is this the json output of the debug task or the content of the result.json file?

Also, why are you using an intermediate file?  
Why not have that vmware\_dvs\_portgroup\_info task be prior to the ones in the playbook you posted?

---

<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:** [May 20, 2021, 4:16am UTC](https://forum.ansible.com/t/parsing-json-files-with-ansible/35198/3 "2021-05-20T04:16:12Z")

</div>

What result do you expect?

The attribute \*DVS-Dev-Ork2\* is a list of dictionaries. There are  
many options on how to proceed. For example, you can iterate the list  
and display \*portgroups\* along with the switch name

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.dvswitch\_name }} {{ item.key }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ dvs.dvs\_portgroup\_info['DVS-Dev-Ork2'] }}

Or, you can create a list of \*portgroups\*, e.g.

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;portgroups: "{{ dvs.dvs\_portgroup\_info['DVS-Dev-Ork2']|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map(attribute='key')|list }}"

---

<div class="post-metadata">

**Author:** ![Rodrigues](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@Rodrigues](https://forum.ansible.com/u/Rodrigues)\
**Post date:** [May 20, 2021, 6:22am UTC](https://forum.ansible.com/t/parsing-json-files-with-ansible/35198/4 "2021-05-20T06:22:42Z")

</div>

Hi Dick, it’s the content of the result.json

I do have a task that connects to the server and dumps the results.json.  
For ease of troubleshooting it’s quicker to run it from a file but the end result is to get it directly from the mentioned task.

---

<div class="post-metadata">

**Author:** ![Rodrigues](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@Rodrigues](https://forum.ansible.com/u/Rodrigues)\
**Post date:** [May 20, 2021, 6:49am UTC](https://forum.ansible.com/t/parsing-json-files-with-ansible/35198/5 "2021-05-20T06:49:44Z")

</div>

Hi Vladimir,

Thanks for your help! Basically I’m looking to gather portgroup\_name and vlanid to use on the next task. What will be the best way to proceed?

---

<div class="post-metadata">

**Author:** ![Andre\_Liebigt](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@Andre\_Liebigt](https://forum.ansible.com/u/Andre_Liebigt)\
**Post date:** [May 21, 2021, 6:56am UTC](https://forum.ansible.com/t/parsing-json-files-with-ansible/35198/6 "2021-05-21T06:56:37Z")

</div>

Hi,

I would suggest the following solution to extract the required information:

- hosts: 127.0.0.1  
connection: local  
vars:  
dvs: “{{ lookup(‘file’, ‘result.json’) }}”  
tasks:
- name: Simple debug.  
debug:  
msg: “{{ dvs.dvs\_portgroup\_info["DVS-Dev-Ork2"] }}”  
tags:
- G2

# VERSION 1

- name: Extract portgroup info  
set\_fact:  
port\_groups: “{{ dvs | json\_query(query) }}”  
vars:  
query: |  
dvs\_portgroup\_info.\*.{  
portgroup\_name: portgroup\_name,  
vlan\_id: vlan\_info.vlan\_id  
}

- debug:  
var: port\_groups

# VERSION 2

- name: Extract portgroups alternative  
set\_fact:  
port\_groups\_alt: “{{ (port\_groups\_alt | default()) + [port\_group] }}”  
vars:  
port\_group:  
portgroup\_name: “{{ item.portgroup\_name }}”  
vlan\_id: “{{ item.vlan\_info.vlan\_id }}”  
loop: “{{ dvs.dvs\_portgroup\_info[‘DVS-Dev-Ork2’] }}”  
loop\_control:  
label: “{{ item.portgroup\_name }}”

- debug:  
var: port\_groups\_alt

This playbook contains two alternative attempts. The first is using JMESPath via json\_query and is a more declarative approach, if you will. The second is a plain looped set\_fact.

Both have the same result: a list of dictionaries with portgroup\_name and vlan\_id:

TASK [Extract portgroup info] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“port\_groups”: [  
{  
“portgroup\_name”: “Test - 192.168.20.0\_24 - VMotion - 1”,  
“vlan\_id”: “20”  
},  
{  
“portgroup\_name”: “Test - 192.168.60.0\_24 - Management”,  
“vlan\_id”: “0”  
},  
{  
“portgroup\_name”: “Test - 192.168.30.0\_24 - VSAN”,  
“vlan\_id”: “25”  
},  
{  
“portgroup\_name”: “DVS-Dev-Ork2-DVUplinks-11”,  
“vlan\_id”: [  
“0-4094”  
]  
}  
]  
}

TASK [Extract portgroups alternative] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> (item=Test - 192.168.20.0\_24 - VMotion - 1)  
ok: [127.0.0.1] =\> (item=Test - 192.168.60.0\_24 - Management)  
ok: [127.0.0.1] =\> (item=Test - 192.168.30.0\_24 - VSAN)  
ok: [127.0.0.1] =\> (item=DVS-Dev-Ork2-DVUplinks-11)

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“port\_groups\_alt”: [  
{  
“portgroup\_name”: “Test - 192.168.20.0\_24 - VMotion - 1”,  
“vlan\_id”: “20”  
},  
{  
“portgroup\_name”: “Test - 192.168.60.0\_24 - Management”,  
“vlan\_id”: “0”  
},  
{  
“portgroup\_name”: “Test - 192.168.30.0\_24 - VSAN”,  
“vlan\_id”: “25”  
},  
{  
“portgroup\_name”: “DVS-Dev-Ork2-DVUplinks-11”,  
“vlan\_id”: [  
“0-4094”  
]  
}  
]  
}

The json\_query version would even work, if there are more than one object in dvs.dvs\_portgroup\_info.  
The last entry may need additional handling as it contains a list of vlan\_ids.

Cheers,  
André

---

<div class="post-metadata">

**Author:** ![Rodrigues](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@Rodrigues](https://forum.ansible.com/u/Rodrigues)\
**Post date:** [May 21, 2021, 1:52pm UTC](https://forum.ansible.com/t/parsing-json-files-with-ansible/35198/7 "2021-05-21T13:52:23Z")

</div>

Hi André,

Thank you, this works perfectly except for one thing (which i’m starting to think its a bug in the ansible module)

I will consume the variables you helped me sort in another task as per below:  
Now, if i use item.portgroup\_name it fails, feels like its parsing the whole dict and not just the name.  
However if i use any other attribute, say vlanid it will work. Any ideas?

This fails:

- name: Create Segments  
nsxt\_policy\_segment:  
hostname: “{{ hostname }}”  
username: “{{ username }}”  
password: “{{ password }}”  
validate\_certs: False  
display\_name: “SEG-{{ item.portgroup\_name }}”  
vlan\_ids:
- “{{ item.vlan\_id }}”  
transport\_zone\_display\_name: “{{ siteid }}-vlan-transport-zone”  
state: “{{ state }}”  
delegate\_to: localhost  
loop: “{{ port\_groups }}”  
tags:
- CS

This works:

- name: Create Segments  
nsxt\_policy\_segment:  
hostname: “{{ hostname }}”  
username: “{{ username }}”  
password: “{{ password }}”  
validate\_certs: False  
display\_name: “SEG-{{ item.vlan\_id }}” \<\<- Only change  
vlan\_ids:
- “{{ item.vlan\_id }}”  
transport\_zone\_display\_name: “{{ siteid }}-vlan-transport-zone”  
state: “{{ state }}”  
delegate\_to: localhost  
loop: “{{ port\_groups }}”  
tags:
- CS

Error:

TASK [Create Segments] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /root/scripts/nsx-t/script4/nsxt-automation/1.yaml:38  
 ESTABLISH LOCAL CONNECTION FOR USER: root  
 EXEC /bin/sh -c ‘echo ~root && sleep 0’  
 EXEC /bin/sh -c ‘( umask 77 && mkdir -p “`echo /root/.ansible/tmp`”&& mkdir “`echo /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905`” && echo ansible-tmp-1621604976.9795952-26415-266025818716905=“`echo /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905`” ) && sleep 0’  
Using module file /root/scripts/nsx-t/script4/nsxt-automation/library/nsxt\_policy\_segment.py  
 PUT /root/.ansible/tmp/ansible-local-261381voivn84/tmpcmt7bpon TO /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905/AnsiballZ\_nsxt\_policy\_segment.py  
 EXEC /bin/sh -c ‘chmod u+x /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905/ /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905/AnsiballZ\_nsxt\_policy\_segment.py && sleep 0’  
 EXEC /bin/sh -c ‘/usr/bin/python3 /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905/AnsiballZ\_nsxt\_policy\_segment.py && sleep 0’  
 EXEC /bin/sh -c ‘rm -f -r /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905/ \> /dev/null 2\>&1 && sleep 0’  
The full traceback is:  
File “/tmp/ansible\_nsxt\_policy\_segment\_payload\_5\_a4rdcu/ansible\_nsxt\_policy\_segment\_payload.zip/ansible/module\_utils/nsxt\_base\_resource.py”, line 713, in \_send\_request\_to\_API  
ignore\_errors=ignore\_error, method=method, data=data)  
File “/tmp/ansible\_nsxt\_policy\_segment\_payload\_5\_a4rdcu/ansible\_nsxt\_policy\_segment\_payload.zip/ansible/module\_utils/policy\_communicator.py”, line 143, in request  
ca\_path=self.ca\_path)  
File “/tmp/ansible\_nsxt\_policy\_segment\_payload\_5\_a4rdcu/ansible\_nsxt\_policy\_segment\_payload.zip/ansible/module\_utils/urls.py”, line 1399, in open\_url  
unredirected\_headers=unredirected\_headers)  
File “/tmp/ansible\_nsxt\_policy\_segment\_payload\_5\_a4rdcu/ansible\_nsxt\_policy\_segment\_payload.zip/ansible/module\_utils/urls.py”, line 1304, in open  
return urllib\_request.urlopen(request, None, timeout)  
File “/usr/lib64/python3.6/urllib/request.py”, line 223, in urlopen  
return opener.open(url, data, timeout)  
File “/usr/lib64/python3.6/urllib/request.py”, line 526, in open  
response = self.\_open(req, data)  
File “/usr/lib64/python3.6/urllib/request.py”, line 544, in \_open  
‘\_open’, req)  
File “/usr/lib64/python3.6/urllib/request.py”, line 504, in \_call\_chain  
result = func(\*args)  
File “/tmp/ansible\_nsxt\_policy\_segment\_payload\_5\_a4rdcu/ansible\_nsxt\_policy\_segment\_payload.zip/ansible/module\_utils/urls.py”, line 483, in https\_open  
return self.do\_open(self.\_build\_https\_connection, req)  
File “/usr/lib64/python3.6/urllib/request.py”, line 1349, in do\_open  
encode\_chunked=req.has\_header(‘Transfer-encoding’))  
File “/usr/lib64/python3.6/http/client.py”, line 1254, in request  
self.\_send\_request(method, url, body, headers, encode\_chunked)  
File “/usr/lib64/python3.6/http/client.py”, line 1265, in \_send\_request  
self.putrequest(method, url, \*\*skips)  
File “/usr/lib64/python3.6/http/client.py”, line 1127, in putrequest  
raise InvalidURL(f"URL can’t contain control characters. {url!r} "  
failed: [127.0.0.1] (item={‘portgroup\_name’: ‘Test - 192.168.20.0\_24 - VMotion - 1’, ‘vlan\_id’: ‘20’}) =\> {  
“ansible\_loop\_var”: “item”,  
“changed”: false,  
“invocation”: {  
“module\_args”: {  
“achieve\_subresource\_state\_if\_del\_parent”: false,  
“address\_bindings”: null,  
“admin\_state”: “UP”,  
“advanced\_config”: null,  
“bridge\_profiles”: null,  
“ca\_path”: null,  
“connectivity\_path”: null,  
“create\_or\_update\_subresource\_first”: false,  
“delete\_subresource\_first”: true,  
“description”: null,  
“dhcp\_config\_path”: null,  
“do\_wait\_till\_create”: false,  
“domain\_name”: null,  
“enforcementpoint\_id”: null,  
“extra\_configs”: null,  
“hostname”: “nsx-dev-Ork2.local”,  
“id”: null,  
“l2\_extension”: null,  
“mac\_pool\_id”: null,  
“metadata\_proxy\_paths”: null,  
“nsx\_cert\_path”: null,  
“nsx\_key\_path”: null,  
“overlay\_id”: null,  
“password”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“port”: 443,  
“replication\_mode”: “MTEP”,  
“request\_headers”: null,  
“segment\_ports”: null,  
“site\_id”: null,  
“state”: “present”,  
“subnets”: null,  
“tags”: null,  
“tier0\_display\_name”: null,  
“tier0\_id”: null,  
“tier1\_display\_name”: null,  
“tier1\_id”: null,  
“transport\_zone\_display\_name”: “Ork2-vlan-transport-zone”,  
“transport\_zone\_id”: null,  
“username”: “admin”,  
“validate\_certs”: false,  
“vlan\_ids”: [  
“20”  
]  
}  
},  
“item”: {  
“portgroup\_name”: “Test - 192.168.20.0\_24 - VMotion - 1”,  
“vlan\_id”: “20”  
},  
“msg”: "Received URL can’t contain control characters. ‘/policy/api/v1/infra/segments/SEG-Test - 192.168.20.0\_24 - VMotion - 1’ (found at least ’ ') from NSX Manager. Please try again. "  
}

---

<div class="post-metadata">

**Author:** ![Rodrigues](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@Rodrigues](https://forum.ansible.com/u/Rodrigues)\
**Post date:** [May 24, 2021, 1:06pm UTC](https://forum.ansible.com/t/parsing-json-files-with-ansible/35198/8 "2021-05-24T13:06:51Z")

</div>

Got it working, turned out to be spaces in the portgroup\_name values. ie. “Test - 192.168.20.0\_24 - VMotion - 1”  
I’ve renamed the portgroups and then it worked fine.

Any ideas how i can escape the dict values so i wont have to rename the portgroups?
