-1

I want to delete the JSON key which contains empty object inside an array...
This is my JSON structure:

{
    "CreateSubnet": {
        "description": "Creatingasubnet.",
        "input": {
            "body": {
                "subnet": {
                    "network_id": "sdfsdfds",
                    "ip_version": "sdfdsfs",
                    "cidr": "sdfsdf",
                    "allocation_pools": [
                        {}
                    ]
                }
            }
        },
        "action": "neutron.create_network",
        "publish": {},
        "on-success": []
    }
}

Please help me to delete the element "allocation_pools"..
I never want this key and this value ...

The expected output JSON will be look like...

{
    "CreateSubnet": {
        "description": "Creatingasubnet.",
        "input": {
            "body": {
                "subnet": {
                    "network_id": "sdfsdfds",
                    "ip_version": "sdfdsfs",
                    "cidr": "sdfsdf"
                }
            }
        },
        "action": "neutron.create_network",
        "publish": {},
        "on-success": []
    }
}

Note this JSON is from external source it changes dynamically...
So please tell me how to validate the JSON key contains Array...
if yes , Check whether it has empty object or not ....
This is my question

Himanshu
  • 4,327
  • 16
  • 31
  • 39
sangeeth kumar
  • 319
  • 2
  • 7
  • 23
  • 2
    possible duplicate of [How do I remove all null and empty string values from a json object?](http://stackoverflow.com/questions/23774231/how-do-i-remove-all-null-and-empty-string-values-from-a-json-object) – renakre Apr 13 '15 at 04:53

1 Answers1

-1

Use the delete operator to remove the property.

var obj = {
    "CreateSubnet": {
        "description": "Creatingasubnet.",
        "input": {
            "body": {
                "subnet": {
                    "network_id": "sdfsdfds",
                    "ip_version": "sdfdsfs",
                    "cidr": "sdfsdf",
                    "allocation_pools": [
                        {}
                    ]
                }
            }
        },
        "action": "neutron.create_network",
        "publish": {},
        "on-success": []
    }
}
delete (obj.CreateSubnet.input.body.subnet.allocation_pools);
document.getElementById("output").innerHTML = JSON.stringify(obj)
<div id="output"></div>
Barmar
  • 741,623
  • 53
  • 500
  • 612