Can you tell me how to remove all null objects in json using javascript?
I also need to remove nested objects with null/empty keys.
{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "text": null, "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "definition": null }, "GlossSee": "markup", "window": { "title": "Sample Konfabulator Widget", "description": "" } } } }, "image": { "src": null, "name": null, "alignment": null }, "text": { "data": "Click Here", "size": null, "style": "bold", "name": "text1", "hOffset": "", "vOffset": "", "alignment": "center", "onMouseUp": null } } }
The required output is as follows:
{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook." }, "GlossSee": "markup", "window": { "title": "Sample Konfabulator Widget" } } } }, "text": { "data": "Click Here", "style": "bold", "name": "text1", "alignment": "center" } } }
How to recursively delete objects with null or empty keys in the entire json.
Just like the image
object, its keys have null or null values.
You can get closer results using
replacer
/reviver
in
JSON.stringify(value, Replacer)/
JSON.parse ( Text, Resurrectionist)Example using JSON.stringify