# PostgreSQL query and dealing with results

**URL:** <https://forum.ansible.com/t/postgresql-query-and-dealing-with-results/23469>\
**Category:** Ansible Developer\
**Created:** [January 16, 2017, 8:55pm UTC](https://forum.ansible.com/t/postgresql-query-and-dealing-with-results/23469 "2017-01-16T20:55:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Tardif](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@Christian\_Tardif](https://forum.ansible.com/u/Christian_Tardif)\
**Post date:** [January 16, 2017, 8:55pm UTC](https://forum.ansible.com/t/postgresql-query-and-dealing-with-results/23469/1 "2017-01-16T20:55:06Z")

</div>

Hi,

I’m trying to build an ansible playbook that would be able to execute a SQL query and then parse the command output to play with the data and update some stuff from it.

My SQL request looks like:

SELECT row\_to\_json(tmp) FROM ( SELECT distinct … ) tmp;

SO far so good. I’m getting a whole lot of output… My only problem is that… the output comes as a list of items, each of them looking like:

{"hostname":"172.27.2.176","string":"public","version":"1"}

Full result.stdout\_lines output looks like:

[ " {"hostname":"172.27.2.176","string":"public","version":"1"}“,  
" {"hostname":"172.27.7.178","string":"private","version":"1"}”,  
" {"hostname":"172.27.1.174","string":"public","version":"2"}"  
]

My problem… the backslashes. The json parser is unable, as it seem, to understand the json construct when there are backslashes. As least, as it seem. If I try to debug it with debug (and typing, for example, msg=“{{ item.hostname }}”, or msg=“{{ item[‘hostname’] }}”, or even msg=“{{ item.[hostname] }}”, it just keeps telling me that I’m trying to use a variable that is undefined.

In fact, I’m not sure that this is the backslashes which are the issues. Maybe I’m not accessing the output the right way ?

Thanks,

Christian
