# Any data file as JSON?

**URL:** <https://forum.ansible.com/t/any-data-file-as-json/15252>\
**Category:** Ansible Project\
**Created:** [May 16, 2014, 8:10am UTC](https://forum.ansible.com/t/any-data-file-as-json/15252 "2014-05-16T08:10:07Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Markus\_Amalthea\_Magn](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Markus\_Amalthea\_Magn](https://forum.ansible.com/u/Markus_Amalthea_Magn)\
**Post date:** [May 16, 2014, 8:10am UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/1 "2014-05-16T08:10:07Z")

</div>

Hey all,

What does it mean in the changelog for Ansible 1.6 that “Any data file can also be JSON”? I figured this could be playbooks, but apparently not. What are “data files”?

Best regards,

Markus

---

<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:** [May 16, 2014, 12:31pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/2 "2014-05-16T12:31:53Z")

</div>

stuff loaded as inventory variable files (group\_vars/|host\_vars/) or with var\_files or roles defaults|vars/main.yml or the include\_vars task.​ They are normally yaml which is a superset of json.

---

<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:** [May 16, 2014, 12:41pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/3 "2014-05-16T12:41:38Z")

</div>

Kinda – saying YAML is a superset of JSON is a bit confusing IMHO, but maybe that’s just me. It’s easier to say that YAML can contain inline JSON inside the document.

Here’s an example:

---

<div class="post-metadata">

**Author:** ![Markus\_Amalthea\_Magn](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Markus\_Amalthea\_Magn](https://forum.ansible.com/u/Markus_Amalthea_Magn)\
**Post date:** [May 16, 2014, 12:44pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/4 "2014-05-16T12:44:07Z")

</div>

I have not managed to run a playbook that is JSON, but maybe I missed something. I think YAML is quite annoying in how it handles whitespace/indentation, so I would love to do everything in JSON instead 🙂

---

<div class="post-metadata">

**Author:** ![Markus\_Amalthea\_Magn](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Markus\_Amalthea\_Magn](https://forum.ansible.com/u/Markus_Amalthea_Magn)\
**Post date:** [May 16, 2014, 12:49pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/5 "2014-05-16T12:49:37Z")

</div>

For example, a playbook looking this fails:

{  
“hosts”: “all”  
}

“ERROR: parse error: playbooks must be formatted as a YAML list, got \<type ‘dict’\>”

---

<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:** [May 16, 2014, 12:53pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/6 "2014-05-16T12:53:45Z")

</div>

playbooks are a list of plays, not an object/hash​/dict

---

<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:** [May 16, 2014, 12:53pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/7 "2014-05-16T12:53:59Z")

</div>

This message about YAML here is a bit spurious, it means “playbooks must be formatted as a list”.

You did specify a dictionary…

---

<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:** [May 16, 2014, 1:05pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/8 "2014-05-16T13:05:01Z")

</div>

technically not valid json cause it doesn’t start with { }, but here is a json version of my stash installation playbook, which works with ansible.

[  
{ “hosts”:“stash”, “sudo”:“true”,“roles”:[“oracle\_java\_7”, “stash”]}  
]

​

---

<div class="post-metadata">

**Author:** ![Markus\_Amalthea\_Magn](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Markus\_Amalthea\_Magn](https://forum.ansible.com/u/Markus_Amalthea_Magn)\
**Post date:** [May 16, 2014, 1:07pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/9 "2014-05-16T13:07:47Z")

</div>

Yeah, this works:

[{  
“hosts”: “all”  
}]

---

<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:** [May 16, 2014, 1:24pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/10 "2014-05-16T13:24:48Z")

</div>

The error message is actually correct, it must be a YAML list, json doesn’t work here because ansible starts with lists/arrays not object/hash/dictionaries, so you cannot use json but shorthand yaml (which is what my example is).​

---

<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:** [May 16, 2014, 2:54pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/11 "2014-05-16T14:54:31Z")

</div>

Markus,

Yes please! I think we can remove the word “YAML” here and just say “must be formatted as a list”.

Brian,

Actually starting with a [ is fine in JSON. It needs to be an array of hashes though.

---

<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:** [May 16, 2014, 3:04pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/12 "2014-05-16T15:04:57Z")

</div>

yes, if we follow the ECMA standard, sadly not all json parsers do. from my reading of the code, this ends up being parsed by the yaml interpreter not the json one.​

Also tested the python2.7 json lib:

> > > import json  
> > > data = open(‘test.json’)

> > > j = json.load(data)  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > File “/usr/lib/python2.7/json/ **init**.py”, line 290, in load  
> > > \*\*kw)  
> > > File “/usr/lib/python2.7/json/ **init**.py”, line 338, in loads  
> > > return \_default\_decoder.decode(s)  
> > > File “/usr/lib/python2.7/json/decoder.py”, line 366, in decode  
> > > obj, end = self.raw\_decode(s, idx=\_w(s, 0).end())  
> > > File “/usr/lib/python2.7/json/decoder.py”, line 382, in raw\_decode  
> > > obj, end = self.scan\_once(s, idx)  
> > > ValueError: Expecting property name: line 1 column 5 (char 4)  
> > > import yaml  
> > > y = yaml.load(data)

> > > print y  
> > > [{‘work’: ‘yes!’}]

the file:test.json:

[{ work: ‘yes!’}]

---

<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:** [May 16, 2014, 3:08pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/13 "2014-05-16T15:08:30Z")

</div>

The actual standard:  
[http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf)

The problem is some parsers look for ONLY a leading ‘{’ to determine if it is json, a leading ‘[’ should be just as valid.

---

<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:** [May 16, 2014, 11:19pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/14 "2014-05-16T23:19:25Z")

</div>

I think it failed for you Bryan because you need quotes around “work”, not because of the leading character.

---

<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:** [May 23, 2014, 2:39pm UTC](https://forum.ansible.com/t/any-data-file-as-json/15252/15 "2014-05-23T14:39:51Z")

</div>

ah, yes, yaml is more forgiving about quoting.
