ansiblejson-query

How to remove null and replace with a string value in ansible


How to remove null and replace with a string novlaue?

I have below output stored in variable out1

{
    "out1": [
        [
            {
                "destination": "dest-a",
                "interface": "e1/1",
                "metric": "10",
                "name": "A"
            },
            {
                "destination": "dest-b",
                "interface": "e1/2",
                "metric": "10",
                "name": "B"
            },
            {
                "destination": "dest-c",
                "interface": null,
                "metric": "10",
                "name": "C"
            },
            {
                "destination": "dest-d",
                "interface": null,
                "metric": "10",
                "name": "B"
            }
        ]
    ]
}

I have a json_query in my code:

- debug: msg="{{out1 |json_query(myquery)}}"
  vars:
     myquery: "[].{dest: destination ,int: interface}"        
  register: out2

Above code will print the following:

{
    "msg": [
        {
            "dest": "dest-a",
            "int": "e1/1"
        },
        {
            "dest": "dest-b",
            "int": "e/12"
        },
        {
            "dest": "dest-c",
            "int": null
        },
        {
            "dest": "dest-d",
            "int": null
        }
    ]
}

I want to replace or remove null with the string novalue.

I looked into some posts and found default("novalue") can do the trick but in my case it is not working. I tried following added default("novalue") to my debug task, but I am getting an error.
I am sure that the error resides in myquery, the way I interpret/understand default() might be wrong and might be used wrongly.

Can anyone help me here please?

- debug: msg="{{out1 |json_query(myquery)}}"
  vars:
     myquery: "[].{dest: destination ,int: interface|default("novalue")}"        
  register: out2

Solution

  • Otherwise, with another JMESPath expression to achieve this, you can use an or expression ||, that will display the value of interface or a string that you are free to define.

    So, given your JSON:

    [
        {
            "destination": "dest-a",
            "interface": "e1/1",
            "metric": "10",
            "name": "A"
        },
        {
            "destination": "dest-b",
            "interface": "e1/2",
            "metric": "10",
            "name": "B"
        },
        {
            "destination": "dest-c",
            "interface": null,
            "metric": "10",
            "name": "C"
        },
        {
            "destination": "dest-d",
            "interface": null,
            "metric": "10",
            "name": "B"
        }
    ]
    

    And the JMESPath query

    [].{dest: destination ,int: interface || 'novalue'}
    

    This yields

    [
      {
        "dest": "dest-a",
        "int": "e1/1"
      },
      {
        "dest": "dest-b",
        "int": "e1/2"
      },
      {
        "dest": "dest-c",
        "int": "novalue"
      },
      {
        "dest": "dest-d",
        "int": "novalue"
      }
    ]
    

    And your task ends up being:

    - debug: 
        msg: "{{ out1 | json_query(_query) }}"
      vars:
         _query: "[].{dest: destination ,int: interface || 'novalue')}"        
      register: out2