Introduction

Modern IT automation often requires handling complex JSON data, whether from APIs, configuration files, or dynamic inputs. Ansible, with its powerful filters like from_json and json_query, provides an elegant way to parse and transform JSON data into actionable formats. This guide walks you through parsing JSON data and transforming it into a structured list in an Ansible playbook.

---

The Scenario

You have a JSON string representing a nested structure, such as:

``json

{

"results": [

{ "instance": { "guest": { "guestState": "running" } }, "item": "foo" },

{ "instance": { "guest": { "guestState": "running" } }, "item": "bar" },

{ "instance": { "guest": { "guestState": "running" } }, "item": "baz" },

{ "instance": { "guest": { "guestState": "running" } }, "item": "qux" },

{ "instance": { "guest": { "guestState": "running" } }, "item": "quux" }

]

}

`

The goal is to transform this JSON into a structured list:

`yaml

formatted_list:

- { name: "foo", guestState: "running" }

- { name: "bar", guestState: "running" }

- { name: "baz", guestState: "running" }

- { name: "qux", guestState: "running" }

- { name: "quux", guestState: "running" }

`

---

The Playbook

Here’s how you can achieve this transformation:

Ansible Code

`yaml

---

  • hosts: localhost

gather_facts: false

tasks:

- name: Define Raw JSON String (Example - in real use, this comes from a variable)

set_fact:

raw_json_string: >

{

"results": [

{ "instance": { "guest": { "guestState": "running" } }, "item": "foo" },

{ "instance": { "guest": { "guestState": "running" } }, "item": "bar" },

{ "instance": { "guest": { "guestState": "running" } }, "item": "baz" },

{ "instance": { "guest": { "guestState": "running" } }, "item": "qux" },

{ "instance": { "guest": { "guestState": "running" } }, "item": "quux" }

]

}

- name: Parse JSON string to dictionary

set_fact:

json_data: "{{ raw_json_string | from_json }}"

- name: Transform JSON to formatted list

set_fact:

formatted_list: "{{ json_data.results | json_query('[].{name: item, guestState: instance.guest.guestState}') }}"

- name: Display formatted list

debug:

msg: "Formatted List: {{ formatted_list }}"

`

---

Explanation

Key Steps

1. Define Raw JSON String:

- Use the set_fact module to define a raw JSON string.

- In real scenarios, this JSON could come from an API, a file, or another source.

2. Parse JSON String:

- The from_json filter converts the JSON string into a Python dictionary, enabling further manipulation.

3. Transform JSON Data:

- Use the json_query filter to extract and restructure the JSON.

- Query: [].{name: item, guestState: instance.guest.guestState}.

- Extracts item as name.

- Extracts instance.guest.guestState