# Save value of json\_query as real integer (without quotes) rather than as string

**URL:** <https://forum.ansible.com/t/save-value-of-json-query-as-real-integer-without-quotes-rather-than-as-string/30825>\
**Category:** Ansible Project\
**Created:** [July 20, 2019, 8:13am UTC](https://forum.ansible.com/t/save-value-of-json-query-as-real-integer-without-quotes-rather-than-as-string/30825 "2019-07-20T08:13:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hauke](https://avatars.discourse-cdn.com/v4/letter/h/5f8ce5/32.png) [@hauke](https://forum.ansible.com/u/hauke)\
**Post date:** [July 20, 2019, 8:13am UTC](https://forum.ansible.com/t/save-value-of-json-query-as-real-integer-without-quotes-rather-than-as-string/30825/1 "2019-07-20T08:13:21Z")

</div>

Hi there,

Currently I try to fetch an integer value of an API and to use that exact value later on to create an object via the same API. The problem is, the API only accepts JSON numbers (integer in this case) as input. Is there any way to tell Ansible to save a value as a “true” integer without the brackets?

My tasks:

`

- name: Foo  
hosts: all  
tasks:

- name: Get the JSON object  
uri:  
url: [https://jira.foo.bar/rest/agile/1.0/board/219/sprint?state=active](https://jira.foo.bar/rest/agile/1.0/board/219/sprint?state=active)  
user: “{{ username }}”  
password: “{{ password }}”  
force\_basic\_auth: yes  
body\_format: json  
register: response  
delegate\_to: localhost

- name: Show important part of response  
debug:  
msg: “{{ response.json }}”

- name: Try to filter out ID without brackets  
debug:

# msg: “{{ response.json |json\_query(‘values[0].id’) |int }}”

# msg: “{{ response.json |json\_query(‘values[0].id’) }}”

# msg: “{{ response.json |json\_query(‘values[0].id’) | to\_json }}”

# msg: “{{ response.json |json\_query(‘values[0].id’) | to\_json |int }}”

msg: “{{ response.json |json\_query(‘values[0].id’) | int | to\_json }}”

`

This is the output of the first debug task where _values[0].id_ is **341**

`TASK [Show important part of response] **************************************************************************************************************************************************************************************************************************************** ok: [test] => { "msg": { "isLast": true, "maxResults": 50, "startAt": 0, "values": [{ "endDate": "2019-07-22T12:00:00.000+02:00", "goal": "", "id": 341, "name": "2019 KW 29", "originBoardId": 1, "self": "https://jira.foo.bar/rest/agile/1.0/sprint/341", "startDate": "2019-07-15T12:00:21.585+02:00", "state": "active" }] } }`

This is the output of any try of the second debug task, _values[0].id_ is every time **“341”**

`TASK [Try to filter out ID without brackets] ********************************************************************************************************************************************************************************************************************************** ok: [test] => { "msg": "341" }`

But the API doesn’t accept a value of **“341”** , it needs a JSON number instead of a string.

Is there anything I can do to achieve that?

Best regards,  
Hauke

---

<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:** [July 20, 2019, 9:38am UTC](https://forum.ansible.com/t/save-value-of-json-query-as-real-integer-without-quotes-rather-than-as-string/30825/2 "2019-07-20T09:38:17Z")

</div>

Don't filter it through to\_json, following debugs will show that the value is a int

- debug: msg={{ response.json.values.0.id | type\_debug }}  
- debug: msg={{ response.json | json\_query('values[0].id') | type\_debug }}

---

<div class="post-metadata">

**Author:** ![hauke](https://avatars.discourse-cdn.com/v4/letter/h/5f8ce5/32.png) [@hauke](https://forum.ansible.com/u/hauke)\
**Post date:** [July 22, 2019, 7:07am UTC](https://forum.ansible.com/t/save-value-of-json-query-as-real-integer-without-quotes-rather-than-as-string/30825/3 "2019-07-22T07:07:17Z")

</div>

Hi Kai,

that’s interesting. It really is an integer, but how can I achieve it to print it like that?

For instance something like

`

- name: Foo  
hosts: all  
vars:  
test\_int: 100  
tasks:
- name: Print test\_int  
debug:  
msg: “{{ test\_int }}”

`

prints the value without brackets. So I guess it should be possible in general to just print numbers without any brackets.

`TASK [Print test_int] *********************************************************************************************************************************************************************************************************** ok: [test] => { "msg": 100 }`

Best regards,  
Hauke

---

<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:** [July 22, 2019, 8:56am UTC](https://forum.ansible.com/t/save-value-of-json-query-as-real-integer-without-quotes-rather-than-as-string/30825/4 "2019-07-22T08:56:08Z")

</div>

For dedicated use cases/playbook I tend to set ANSIBLE\_JINJA2\_NATIVE to true:

[https://docs.ansible.com/ansible/latest/reference\_appendices/config.html#default-jinja2-native](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-jinja2-native)

But setting that globally in ansible.cfg had some unintended side  
effect, as up to now a lot of ansible code (roles modules etc) had  
been coded towards the old behaviour.  
In short, YMMV.

Dick

---

<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:** [July 23, 2019, 7:12am UTC](https://forum.ansible.com/t/save-value-of-json-query-as-real-integer-without-quotes-rather-than-as-string/30825/5 "2019-07-23T07:12:22Z")

</div>

When using the variables Ansible/Jinja will mostly do the right thing.

Screen output is just for informational purposes to the user and is heavy formatted by the callback plugin[1].  
The default callback plugin is call default and have quotes everywhere.  
But there are many[2] to choose from and some is better than other to print information on screen.

Personally I use the debug[3] one since it gives a more human readable output and it's very verbose so any error will have all the information I need.

[1] [https://docs.ansible.com/ansible/2.8/plugins/callback.html](https://docs.ansible.com/ansible/2.8/plugins/callback.html)  
[2] [https://docs.ansible.com/ansible/2.8/plugins/callback.html#plugin-list](https://docs.ansible.com/ansible/2.8/plugins/callback.html#plugin-list)  
[3] [https://docs.ansible.com/ansible/2.8/plugins/callback/debug.html](https://docs.ansible.com/ansible/2.8/plugins/callback/debug.html)
