# How to load a json data?

**URL:** <https://forum.ansible.com/t/how-to-load-a-json-data/19542>\
**Category:** Ansible Project\
**Created:** [October 19, 2015, 12:47am UTC](https://forum.ansible.com/t/how-to-load-a-json-data/19542 "2015-10-19T00:47:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivan\_Smirnov](https://avatars.discourse-cdn.com/v4/letter/i/a4c791/32.png) [@Ivan\_Smirnov](https://forum.ansible.com/u/Ivan_Smirnov)\
**Post date:** [October 19, 2015, 12:47am UTC](https://forum.ansible.com/t/how-to-load-a-json-data/19542/1 "2015-10-19T00:47:04Z")

</div>

Hi

I need to parse JSON configuration files. The directory sctructure is like this:

> ├── configs  
> │ ├── dir1  
> │ │ └── config.json  
> │ └── dir2  
> │ └── config.json  
> │ …  
> │ └── dirN  
> │ └── config.json

N is not known prior to proceeding. Every config.json has the same set of variables (variables with same name, but different values) defining different virtual hosts like:  
config.json:

> {  
> listen: 8081  
> name: dev1  
> cache: no  
> }

How should I write Ansible playbook to process such kind of data and generate virtual host configs from them?

I tried with\_fileglob loops but they don`t work. Maybe because with_fileglob is for one-dir listings. Module "find" is`n included in my version of Ansible (1.9.4). And there`s a bigger problem: how to parse this kind of data and set Ansible variables to use it?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 19, 2015, 5:37pm UTC](https://forum.ansible.com/t/how-to-load-a-json-data/19542/2 "2015-10-19T17:37:26Z")

</div>

Just load them as you would load yaml files, json is a subset of yaml  
so vars\_files and include\_vars will read json files.

---

<div class="post-metadata">

**Author:** ![Ivan\_Smirnov](https://avatars.discourse-cdn.com/v4/letter/i/a4c791/32.png) [@Ivan\_Smirnov](https://forum.ansible.com/u/Ivan_Smirnov)\
**Post date:** [October 20, 2015, 5:32pm UTC](https://forum.ansible.com/t/how-to-load-a-json-data/19542/3 "2015-10-20T17:32:15Z")

</div>

Thank you, Brian.

But how do I do this provided that (as I mentioned in the first post):

1. Files to include with vars\_files ot include vars are located by the path configs\*\config.json, and this path should be processed dynamically (the names of directories \* and the number of directories are not known prior to proceeding). How do I load them dynamically travelling through the structure configs/_/_.json? How to go through this structure of files dynamically?

2. Just loading each config.json does not solve the problem since in every config.json variables have the same name (as I`ve mentioned it in my first post). E.g. variable 'name' from the configs/dir1/config.json will be overwritten by the variable 'name' loaded from configs/dir2/config.json . The solution could be add values from each config to some array. But I don`t understand how is it possible to fill this array. Another solution could be generate vhost configs at each step after loading every config, but since Ansible does not support multiple tasks within one loop (as far as I know) I don`t understand how to do that.

So, any ideas?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 20, 2015, 5:34pm UTC](https://forum.ansible.com/t/how-to-load-a-json-data/19542/4 "2015-10-20T17:34:48Z")

</div>

1. can be solved using either a find task or a lookup (fileglob, pipe, etc)

2. you can set facts on each file using the path for namespacing

---

<div class="post-metadata">

**Author:** ![Ivan\_Smirnov](https://avatars.discourse-cdn.com/v4/letter/i/a4c791/32.png) [@Ivan\_Smirnov](https://forum.ansible.com/u/Ivan_Smirnov)\
**Post date:** [October 21, 2015, 12:18pm UTC](https://forum.ansible.com/t/how-to-load-a-json-data/19542/5 "2015-10-21T12:18:36Z")

</div>

Thank you, Brian.

I can`t use find because it`s only itroduced in version 2.0.

And the main limitation now is inability to use several actions in the same loop.

I tried something like

set\_fact: sites\_array = “{{ include\_vars: {{item}} }}”  
with\_fileglob:

- configs/\*.config.json

But this kind of construction is not allowed (several actions at once: incude\_vars + set\_fact). And with\_fileglob does`n work for files that are not in the same directory.

So the question is:  
How I should build Ansible instructions to: (1) walk through the file structure configs/\*.config.json, (2) load each json config, and (3) fill in array sites\_array with data from each config.

Seems to be several actions for the same loop, which is not allowed. Is it possible to do it using Ansible?

I would be grateful if someone shows me the correct way to do this 🙂
