jsonansiblexml-parsing

How to read a value from JSON using Ansible?


Have the below Sample JSON:

{
"fulfilmentOrderItems": [
    {
        "customerDates": {
        },
        "instanceCharacteristics": [
            {
                "name": "VLAN",
                "value": "24",
                "action": "Cancel"
            }
        ],
        "orderLineId": {
            "value": "123456-10",
            "source": "AS"
        }
    }
    ],
    "orderId": {
        "identifier": {
            "value": "1234567-96_11",
            "source": "AS"
        },
        "version": 1
    }
}

How do I extract a certain value like the name i.e VLAN or the value (12321321412a-10)?

Tried using

- name: Read data from Json
  set_fact:
    Value: "{{ lookup('file', 'POST.json') | from_json }}"

- name: Get the value from JSON
  debug:
    msg: "The value is  {{ value.value }}"

I get the below output

TASK [Get the value from JSON] ************************
fatal:FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'value' is undefined

Solution

  • Q: "How do we replace the value i.e 24? I need to update the value and pass the JSON."

    It is not possible to update variables in places, however, you could use update_fact module – Update currently set facts to create an other data structure.

    A minimal example playbook

    ---
    - hosts: localhost
      become: false
      gather_facts: false
    
      tasks:
    
        - include_vars:
            file: sample.json
            name: readFromFile
    
        - debug:
            msg: "{{ readFromFile }}"
    
        - debug:
            msg: "{{ ((readFromFile.fulfilmentOrderItems | first).instanceCharacteristics | first).value }}"
    
        - name: Update the fact
          ansible.utils.update_fact:
            updates:
              - path: readFromFile.fulfilmentOrderItems[0].instanceCharacteristics[0].value
                value: '0'
          register: updated
    
        - debug:
            msg: "{{ updated.readFromFile }}"
    

    will result into an output of

    TASK [include_vars] ***********
    ok: [localhost]
    
    TASK [debug] *****************
    ok: [localhost] =>
      msg:
        fulfilmentOrderItems:
        - customerDates: {}
          instanceCharacteristics:
          - action: Cancel
            name: VLAN
            value: '24'
          orderLineId:
            source: AS
            value: 123456-10
        orderId:
          identifier:
            source: AS
            value: 1234567-96_11
          version: 1
    
    TASK [debug] *****************
    ok: [localhost] =>
      msg: '24'
    
    TASK [Update the fact] *******
    changed: [localhost]
    
    TASK [debug] *****************
    ok: [localhost] =>
      msg:
        fulfilmentOrderItems:
        - customerDates: {}
          instanceCharacteristics:
          - action: Cancel
            name: VLAN
            value: '0'
          orderLineId:
            source: AS
            value: 123456-10
        orderId:
          identifier:
            source: AS
            value: 1234567-96_11
          version: 1
    

    Further Readings