How to remove the extra curly bracket while using jinja2 if condition in Ansible

I am creating a new JSON list using the jinja2 template and a json(base.json) file. The output is having extra curly brackets while iterating the json file which doesnot meet the condition to have a hubs array inside the json file.

strong text

[
    {
        "ansible_loop_var": "item",
        "item": {
            "subnet": "192.168.127.0/24",
            "useVpn": false
        },
        "json": {
            "mode": "none"
        },
        "msg": "OK (unknown bytes)"
    
    },
    {
        "ansible_loop_var": "item",
        "item": {
            
            "subnet": "192.168.128.0/24",
            "useVpn": false
        },
        "json": {
            "mode": "none"
        },
        "msg": "OK (unknown bytes)",
        "pragma": "no-cache",
        "redirected": false,
        "server": "nginx/1.21.4",
        "status": 200
    },
    {
        "ansible_loop_var": "item",
        "invocation": {
            "module_args": {
                
                "status_code": [
                    200
                ],
                
                "validate_certs": false
            }
        },
        "item": {
            "name": "MGMT",
            "networkId": "987654321",
            "subnet": "1.1.1.1/28",
            "useVpn": false
        },
        "json": {
            "hubs": [
                {
                    "hubId": "123456789",
                    "useDefaultRoute": false
                }
            ],
            "mode": "spoke",
            "subnets": [
                {
                    "localSubnet": "1.1.1.1/28",
                    "useVpn": false
                }
            ]
        },
        "msg": "OK (unknown bytes)"
    }
]

Jinja template

[
    {% for dict_item in base %}
    {% if dict_item['json']['hubs'] is defined %}
    {
    "netid": "{{ dict_item['item']['networkId'] }}",
    "mgmt-subnet": "{{ dict_item['item']['subnet'] }}",
    "mode": "{{ dict_item['json']['mode'] }}",
    {% if dict_item['json']['hubs'] is defined %}
    "hubs": [
            {% for item in dict_item['json']['hubs'] %}
            {
            "hubId": "{{ item['hubId'] }}",
            "useDefaultRoute": {{ item['useDefaultRoute'] | lower}}
            }{% if not loop.last %},
            {% endif %}
            {% endfor %}
            ],
    {% endif %}
    {% if dict_item['json']['subnets'] is defined %}
    "subnets": [
            {% for item in dict_item['json']['subnets'] %}
            {
            "localSubnet": "{{ item['localSubnet'] }}",
            "useVpn": {{ item['useVpn'] | lower}}
            }{% if not loop.last %},
            {% endif %}
            {% endfor %}
            ]
    {% endif %}
    {% endif %}
    }{% if not loop.last %},
    {% endif %}
    {% endfor %}
]

Current Output JSON

[
},
    },
    {
"netid": "987654321",
"mgmt-subnet": "10.28.64.128/28",
"mode": "spoke",
"hubs": [
            {
"hubId": "123456789",
"useDefaultRoute": false
}                        ],
"subnets": [
            {
"localSubnet": "1.1.1.1/28",
"useVpn": false
}                       ]
}    ]

Expected Output JSON Need to remove the initial extra curly bracket while iterating over the base.json which doesnot contain array as hubs

[
    {
"netid": "987654321",
"mgmt-subnet": "10.28.64.128/28",
"mode": "spoke",
"hubs": [
            {
"hubId": "123456789",
"useDefaultRoute": false
}                        ],
"subnets": [
            {
"localSubnet": "1.1.1.1/28",
"useVpn": false
}                       ]
}    ]

you could write this jinja2 template: you loop only if dict_item['json']['hubs'] is defined

[
    {% for dict_item in base if dict_item['json']['hubs'] is defined %}
    {
    "netid": "{{ dict_item['item']['networkId'] }}",
    "mgmt-subnet": "{{ dict_item['item']['subnet'] }}",
    "mode": "{{ dict_item['json']['mode'] }}",
    "hubs": [
            {%      for item in dict_item['json']['hubs'] %}
            {
            "hubId": "{{ item['hubId'] }}",
            "useDefaultRoute": {{ item['useDefaultRoute'] | lower}}
            }{% if not loop.last %},
             {%  endif %}          
            {%      endfor %}
            ],
    {%          if dict_item['json']['subnets'] is defined %}
    "subnets": [
            {%      for item in dict_item['json']['subnets'] %}
            {
            "localSubnet": "{{ item['localSubnet'] }}",
            "useVpn": {{ item['useVpn'] | lower}}
            }{% if not loop.last %},
             {% endif %}
            {%      endfor %}
            ]
    {%          endif %}
    }{% if not loop.last %},
     {% endif %}
    {% endfor %}
]

result:

[
        {
    "netid": "987654321",
    "mgmt-subnet": "1.1.1.1/28",
    "mode": "spoke",
    "hubs": [
                        {
            "hubId": "123456789",
            "useDefaultRoute": false
            }          
                        ],
        "subnets": [
                        {
            "localSubnet": "1.1.1.1/28",
            "useVpn": false
            }                        ]
        }    ]