0

I have got json from variable like below.

[
{
    "policyName1": {
    "resourceType": "Name1Type",
    "id": "uuid-0000-000-0-1",
    "extraAttr1": 
    [
        {
            "network11": 
            {
                "allowAction": "yes",
                "ipAddresses":
                [
                    "192.168.29.193",
                    "192.168.29.196"
                ],
                "disabled": false,
                "ip_protocol": "IPV4_IPV6"
            }
        }
    ]
    }
},
{
    "policyName2": {
    "resourceType": "name2Type",
    "id": "uuid-0000-000-0-2",
    "extraAttr12": 
    [
        {
            "network12": 
            {
                "allowAction": "yes",
                "ipAddresses":
                [
                    "192.168.29.193",
                    "192.168.29.197"
                ],
                "disabled": false,
                "ip_protocol": "IPV4_IPV6"
            }
        }
    ]
    }
},
{
    "policyName3": {
    "resourceType": "name3Type",
    "id": "uuid-0000-000-0-3",
    "extraAttr2": 
    [
        {
            "network13": 
            {
                "allowAction": "yes",
                "ipAddresses":
                [
                    "192.168.29.191",
                    "192.168.29.195"
                ],
                "disabled": false,
                "ip_protocol": "IPV4_IPV6"
            }
        }
    ]
    }
}

]

As you can see it is a normaln json. I need select (extract) whole object by id. For example, when I put id "uuid-0000-000-0-3" i would like receive object PolicyName3

{
"policyName3": {
"resourceType": "name3Type",
"id": "uuid-0000-000-0-3",
"extraAttr3": 
[
    {
        "netowork13": 
        {
            "allowAction": "yes",
            "ipAddresses":
            [
                "192.168.29.191",
                "192.168.29.195"
            ],
            "disabled": false,
            "ip_protocol": "IPV4_IPV6"
        }
    }
]
}

}

The digit are add automaticly by software and I can not remove them.

Also it is possible to add extra IP address childName.ipAddresses??

Thank you for help

Sebastian
  • 15
  • 4
  • Can you use python for this? – Jack Fleeting Feb 06 '20 at 20:06
  • It's not clear how the "extra IP" should be added. Provide the expected result, please. – Vladimir Botka Feb 07 '20 at 02:26
  • The data in the question are not consistent. The dictionary `policyName3` in the list comprises attribute `extraAttr2` while the same dictionary in the example of the result comprises attribute `extraAttr3`. Thre are typos in the data. – Vladimir Botka Feb 07 '20 at 02:31
  • FWIW, there is no reason for extraAttr3 to be a list when the items are nested dictionaries. It's simply a bad structure introducing redundant complexity. The same is valid for the top list. – Vladimir Botka Feb 07 '20 at 02:39
  • unfortunately I can not use a python. In this case we have 3 elements in one array. I would like take a index of element and modify them - for example add extra ip address extraAttr2.netowork12.ipAddresses – Sebastian Feb 07 '20 at 09:06

1 Answers1

0

Q: "Select (extract) the whole object by id."

A: Given the variable policy_list the tasks below

    - set_fact:
        ext1: "{{ policy_list|
                  map('dict2items')|list|flatten|
                  json_query(query)|
                  items2dict }}"
      vars:
        id: "uuid-0000-000-0-3"
        query: "[?value.id == '{{ id }}']"
    - debug:
        var: ext1

give

    "ext1": {
        "policyName3": {
            "extraAttr2": [
                {
                    "network13": {
                        "allowAction": "yes", 
                        "disabled": false, 
                        "ipAddresses": [
                            "192.168.29.191", 
                            "192.168.29.195"
                        ], 
                        "ip_protocol": "IPV4_IPV6"
                    }
                }
            ], 
            "id": "uuid-0000-000-0-3", 
            "resourceType": "name3Type"
        }
    }
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63