# json\_query problem

**URL:** <https://forum.ansible.com/t/json-query-problem/31578>\
**Category:** Ansible Project\
**Created:** [October 31, 2019, 12:05pm UTC](https://forum.ansible.com/t/json-query-problem/31578 "2019-10-31T12:05:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spiro\_Mitsialis](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Spiro\_Mitsialis](https://forum.ansible.com/u/Spiro_Mitsialis)\
**Post date:** [October 31, 2019, 12:05pm UTC](https://forum.ansible.com/t/json-query-problem/31578/1 "2019-10-31T12:05:59Z")

</div>

I am reading a cmdb for location information and then I’m trying to match on the room number in order to get the locations id. The matching data is coming from a csv file. The play works for rooms that have letters in them but not if it is just numbers. I have not been able to figure out how to make it work.  
Result of cmdb lookup:

`  
“building\_rooms\_result.json”: [  
{  
“buildingName”: “xxx Building”,  
“city”: “Montreal”,  
“commonName”: “”,  
“country”: “Canada”,  
“createdAt”: “2018-07-13T06:03:57.000Z”,  
“fullName”: “Downtown/xxx Building/Floor 0005/5106”,  
“id”: “ck2apr6yb002708380h9h7v3f”,  
“loadedAt”: “2019-10-28T17:48:28.000Z”,  
“locationKey”: “240|0005|5106”,  
“locationType”: “Classroom”,  
“name”: “5106”,  
“parentId”: “ck2aps8cl0e260838rofis4v6”,  
},

{  
“buildingName”: “xxx Building”,  
“city”: “Montreal”,  
“commonName”: “”,  
“country”: “Canada”,  
“createdAt”: “2018-07-13T06:06:32.000Z”,  
“fullName”: “Downtown/xxx Building/Floor 0004/4-TDUCT149”,  
“id”: “ck2apr6zh009g0838abhya4sj”,  
“loadedAt”: “2019-10-28T17:48:29.000Z”,  
“locationKey”: “240|0004|4-TDUCT149”,  
“locationType”: “Ducts”,  
“name”: “4-TDUCT149”,  
“parentId”: “ck2aps8br0diz08383bmk41yj”,  
},

{  
“buildingName”: “xxx Building”,  
“city”: “Montreal”,  
“commonName”: “”,  
“country”: “Canada”,  
“createdAt”: “2018-07-13T06:04:05.000Z”,  
“fullName”: “Downtown/xxx Building/Floor 0001/1-STAIR4”,  
“id”: “ck2apr6zq00bp0838h73fdrzf”,  
“loadedAt”: “2019-10-28T17:48:31.000Z”,  
“locationKey”: “240|0001|1-STAIR4”,  
“locationType”: “Stairs”,  
“name”: “1-STAIR4”,  
“parentId”: “ck2aptbfr0tr9083813gkgava”,  
}

]  
`

This is the json\_query I’m using. If I try to match on 1-STAIR4, it works. If I try to match on 5106 it does not match and I get the default na in the jacks\_list.

`

- name: create new list with all jack info  
set\_fact:  
jacks\_list: “{{ jacks\_list | default() +  
[ { ‘number’: item.value.JACKNUMBER,  
‘locationId’: building\_rooms\_result.json | json\_query(query\_locat) | default(‘na’, True),  
‘telcoId’: telco\_result.json.0.id  
}] }}”  
vars:  
query\_locat: ‘[?name==`{{ item.value.ROOMNUMBER | string }}`].id | [0]’  
loop: “{{ connections.dict | dict2items }}”

`

`  
The csv import data looks like:  
ok: [127.0.0.1] =\> (item={‘key’: u’240D005082’, ‘value’: {u’ACTIVEDATE’: u’01-Dec-00’, u’SHUT’: u’0’, u’LASTNAME’: u’‘, u’VLAN’: u’71’, u’ROOMNUMBER’: u’5102’, u’ROOMID’: u’240D’, u’PORT\_TYPE’: u’GigabitEthernet1/0/37’, u’ACTIVEBY’: u’‘, u’PORT\_GROUP’: u’eng’, u’DEPTNAME’: u’Department of ‘, u’PORT\_NO’: u’1/0/37’, u’FIRSTNAME’: u’S’, u’AUTHORIZEDBY’: u’So, Mr.‘, u’SPEED’: u’auto’, u’FOAPAL’: u’‘, u’DUPLEX’: u’auto’, u’DEPTCODE’: u’00156’, u’BUILDCODE’: u’000240’, u’SWITCHNAME’: u’xxx-5050-sw1’, u’JACKNUMBER’: u’240D005082’, u’F\_ROOMCODE’: u’'}})

ok: [127.0.0.1] =\> (item={‘key’: u’240D005028’, ‘value’: {u’ACTIVEDATE’: u’01-Aug-19’, u’SHUT’: u’0’, u’LASTNAME’: u’IT’, u’VLAN’: u’67’, u’ROOMNUMBER’: u’5110’, u’ROOMID’: u’240D’, u’PORT\_TYPE’: u’GigabitEthernet1/0/34’, u’ACTIVEBY’: u’mary’, u’PORT\_GROUP’: u’eng’, u’DEPTNAME’: u’Department ‘, u’PORT\_NO’: u’1/0/34’, u’FIRSTNAME’: u’ECE’, u’AUTHORIZEDBY’: u’, Mr.‘, u’SPEED’: u’auto’, u’FOAPAL’: u’000000’, u’DUPLEX’: u’auto’, u’DEPTCODE’: u’00156’, u’BUILDCODE’: u’000240’, u’SWITCHNAME’: u’xxxr-5050-sw1’, u’JACKNUMBER’: u’240D005028’, u’F\_ROOMCODE’: u’5110’}})

ok: [127.0.0.1] =\> (item={‘key’: u’240D005280’, ‘value’: {u’ACTIVEDATE’: u’08-Nov-06’, u’SHUT’: u’0’, u’LASTNAME’: u’‘, u’VLAN’: u’820’, u’ROOMNUMBER’: u’5080HLWY’, u’ROOMID’: u’240D’, u’PORT\_TYPE’: u’GigabitEthernet3/0/35’, u’ACTIVEBY’: u’csr3’, u’PORT\_GROUP’: u’noc’, u’DEPTNAME’: u’Network’, u’PORT\_NO’: u’3/0/35’, u’FIRSTNAME’: u’Wireless’, u’AUTHORIZEDBY’: u’NOR’, u’SPEED’: u’auto 10 100’, u’FOAPAL’: u’000000’, u’DUPLEX’: u’auto’, u’DEPTCODE’: u’00043’, u’BUILDCODE’: u’000240’, u’SWITCHNAME’: u’xxxr-5050-sw1’, u’JACKNUMBER’: u’240D005280’, u’F\_ROOMCODE’: u’'}})

`

From the above only 5080HLWY will match to a record in locations. Looks like its an issue of string to int but I cant seem to figure out how and where to convert it. I am using the read\_csv Ansible module to read the csv file.
