Bootstrap FreeKB - Ansible - Parse JSON using from_json
Ansible - Parse JSON using from_json

Updated:   |  Ansible articles

Let's say you have a JSON file, perhaps something like this.

"domains": {
            "foo": {
                "alias": "foo", 
                "region": "us-east-1"
            },
            "bar.example.com": {
                "alias": "bar", 
                "region": "us-east-2"
            }
        }

 

If you parse the JSON file using the shell or command or slurp modules.

---
- hosts: localhost
  tasks:
  - slurp:
      path: /path/to/example.json
    register: slurp

  - debug:
      var: slurp.content | b64decode
...

 

You'll probably get something like this returned.

ok: [localhost] => {
    "slurp.content | b64decode": "domains:\n  foo:\n    alias: foo\n    region: us-east-1\n  bar.example.com:\n    alias: bar\n    region: us-east-2\n"
}

 

from_json can be used to return the JSON as a dictionary of structured data. This is similar to using Python json.loads.

---
- hosts: localhost
  tasks:
  - slurp:
      path: /path/to/example.json
    register: slurp

  - debug:
      var: slurp.content | b64decode | from_json
...

 

Which should now return something like this.

ok: [localhost] => {
    "slurp.content | b64decode | from_json": {
        "domains": {
            "bar.example.com": {
                "alias": "bar", 
                "region": "us-east-2"
            }, 
            "foo": {
                "alias": "foo", 
                "region": "us-east-1"
            }
        }
    }
}

 

And here is how to return the data below the "domains" key.

---
- hosts: localhost
  tasks:
  - slurp:
      path: /path/to/example.json
    register: slurp

  - debug:
      var: (slurp.content | b64decode | from_json).domains
...

 

Or like this.

- debug:
    var: (slurp.content | b64decode | from_json)['domains']

 

Which should return the following.

ok: [localhost] => {
    "(slurp.content | b64decode | from_json).domains": {
        "bar.example.com": {
            "alias": "bar", 
            "region": "us-east-2"
        }, 
        "foo": {
            "alias": "foo", 
            "region": "us-east-1"
        }
    }
}

 

And here is how to return the data below the "foo" key.

---
- hosts: localhost
  tasks:
  - slurp:
      path: /path/to/example.json
    register: slurp

  - debug:
      var: (slurp.content | b64decode | from_json).domains.foo
...

 

Or like this.

- debug:
    var: (slurp.content | b64decode | from_json).domains['foo']

 

Or like this.

- debug:
    var: (slurp.content | b64decode | from_json)['domains'].foo

 

Or like this.

- debug:
    var: (slurp.content | b64decode | from_json)['domains']['foo']

 

Which should return the following.

ok: [localhost] => {
    "(slurp.content | b64decode | from_json).domains.foo": {
        "alias": "foo", 
        "region": "us-east-1"
    }
}

 

Since the bar.example.com key contains periods (dot notation), the bar.example.com must be placed inside of single quoted brackets like this.

- debug:
    var: (slurp.content | b64decode | from_json)['domains']['bar.example.com']

 

Or like this.

- debug:
    var: (slurp.content | b64decode | from_json).domains['bar.example.com']

 

If you were to try something like this.

- debug:
    var: (slurp.content | b64decode | from_json).domainsbar.example.com

 

Something like VARIABLE IS NOT DEFINED may be returned.

ok: [localhost] => {
    "(slurp.content | b64decode | from_json).domains.bar.example.com": "VARIABLE IS NOT DEFINED!"
}

 

The following can be used to loop through the JSON.

For example, with_items to loop through the keys below the "domain" key.

---
- hosts: localhost
  tasks:
  - slurp:
      path: /path/to/example.json
    register: slurp

  - debug:
      var: item
    with_items: "{{ (slurp.content | b64decode | from_json).domains }}"
...

 

Should return the following, just the keys below the "domain" key.

ok: [localhost] => (item=bar.example.com) => {
    "ansible_loop_var": "item", 
    "item": "bar.example.com"
}
ok: [localhost] => (item=foo) => {
    "ansible_loop_var": "item", 
    "item": "foo"
}

 

with_dict to loop through the "domain" key.

---
- hosts: localhost
  tasks:
  - slurp:
      path: /path/to/example.json
    register: slurp

  - debug:
      var: item
    with_dict: "{{ (slurp.content | b64decode | from_json).domains }}"
...

 

Should return the following, all of the keys and values below the "domain" key.

ok: [localhost] => (item={u'key': u'bar.example.com', u'value': {u'alias': u'bar', u'region': u'us-east-2'}}) => {
    "ansible_loop_var": "item", 
    "item": {
        "key": "bar.example.com", 
        "value": {
            "alias": "bar", 
            "region": "us-east-2"
        }
    }
}
ok: [localhost] => (item={u'key': u'foo', u'value': {u'alias': u'foo', u'region': u'us-east-1'}}) => {
    "ansible_loop_var": "item", 
    "item": {
        "key": "foo", 
        "value": {
            "alias": "foo", 
            "region": "us-east-1"
        }
    }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter b3c005 in the box below so that we can be sure you are a human.