# Ansible JSON module?

**URL:** <https://forum.ansible.com/t/ansible-json-module/15825>\
**Category:** Ansible Project\
**Created:** [July 31, 2014, 8:33pm UTC](https://forum.ansible.com/t/ansible-json-module/15825 "2014-07-31T20:33:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_VanGundy](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@Ryan\_VanGundy](https://forum.ansible.com/u/Ryan_VanGundy)\
**Post date:** [July 31, 2014, 8:33pm UTC](https://forum.ansible.com/t/ansible-json-module/15825/1 "2014-07-31T20:33:46Z")

</div>

Is there a module for manipulating JSON data? Say I have a settings file that’s written in JSON, and I’d like to set or modify a value. As a task, this might look like:

`

- name: Modify author name  
json: src=config.json key=author.name value=‘rvang’  
`

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 31, 2014, 9:35pm UTC](https://forum.ansible.com/t/ansible-json-module/15825/2 "2014-07-31T21:35:12Z")

</div>

Nothing in core presently.

I’d probably be open to it. Can’t remember what my comments were WRT this in the past.

Probably needs to be pseudo-XPATHey in nature.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 31, 2014, 9:39pm UTC](https://forum.ansible.com/t/ansible-json-module/15825/3 "2014-07-31T21:39:45Z")

</div>

its soooo easy to write json with jinja templates.​

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 31, 2014, 9:41pm UTC](https://forum.ansible.com/t/ansible-json-module/15825/4 "2014-07-31T21:41:26Z")

</div>

I’m assuming this was a request to replace some keys inside of them.

In most cases yes, if you have a datastructure defined in Ansible - and ansible can read almost any data file as JSON instead of YAML too - not that this matters,

{{ datastructure }}

is all the template needs to be.

I was assuming this was about config file changes, but if you can use a template, it’s better to fully declare the desired end result 99.999% of the time. Good point.

---

<div class="post-metadata">

**Author:** ![Ryan\_VanGundy](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@Ryan\_VanGundy](https://forum.ansible.com/u/Ryan_VanGundy)\
**Post date:** [July 31, 2014, 10:52pm UTC](https://forum.ansible.com/t/ansible-json-module/15825/5 "2014-07-31T22:52:30Z")

</div>

I tend to take the template route almost all the time. This is in regard to modifying JSON-based configuration files. I compose several roles and run them against a host. Each role might do something very distinct, while they all modify a common configuration file or add information to a meta-data file. Since I have several roles none of which know about one-another, yet all need to add some relevant data to a common JSON-based configuration file, it would be convenient to edit this data directly since there’s really no single role from where I can define a master template.

---

<div class="post-metadata">

**Author:** ![Ryan\_VanGundy](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@Ryan\_VanGundy](https://forum.ansible.com/u/Ryan_VanGundy)\
**Post date:** [July 31, 2014, 11:11pm UTC](https://forum.ansible.com/t/ansible-json-module/15825/6 "2014-07-31T23:11:19Z")

</div>

So are there any examples of a playbook or role that loads some JSON data from a file, works with that data, then writes it back out using a simple template like {{ data\_structure }} ? What set of tasks could perform this?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 31, 2014, 11:55pm UTC](https://forum.ansible.com/t/ansible-json-module/15825/7 "2014-07-31T23:55:17Z")

</div>

Not really.

set\_fact mostly allows replacing variables but doesn’t have a good facility for what you speak of.

What you speak of might be dealt with with something like action\_plugin theoretically like “set\_hash\_entry” that took a hash, a key, and value, maybe.  
It gets a little programmy that way though. Not trying to fight it, just not sure it’s the best way.

Maybe others have ideas on how to get creative.

---

<div class="post-metadata">

**Author:** ![Ryan\_VanGundy](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@Ryan\_VanGundy](https://forum.ansible.com/u/Ryan_VanGundy)\
**Post date:** [August 1, 2014, 1:02am UTC](https://forum.ansible.com/t/ansible-json-module/15825/8 "2014-08-01T01:02:05Z")

</div>

I see. Maybe I’ll try my hand at writing a module. I’ll drop a link to a github repo when that happens.
