Skip to content

Instantly share code, notes, and snippets.

@antic183
Last active June 26, 2016 20:18
Show Gist options
  • Save antic183/f7d68f00f1c36e319ca17ab1ce21e1d2 to your computer and use it in GitHub Desktop.
Save antic183/f7d68f00f1c36e319ca17ab1ce21e1d2 to your computer and use it in GitHub Desktop.
clean json (remove undefined nodes)
var jsonDummy = {
key: 'val',
underobj: {
ewssswww: undefined,
asdas: 45105,
ssswww: undefined,
errwew: undefined,
ssswww: 0,
sdfsd: 'dfsdf',
uuunt: {
a: "aaa",
b: [055, 4540, undefined, undefined, undefined, 10510, 4747]
},
dfff: [
5404,
undefined,
undefined,
444777
]
}
}
// before cleaning json
console.info(JSON.stringify(jsonDummy));
function recursiveJsonCleaner(key, value) {
$.each(value, function (i, val) {
if (typeof val === 'object') {
recursiveJsonCleaner(i, val);
} else {
if (typeof val === "undefined") {
if (Array.isArray(value)) {
value.splice(i, 1);
recursiveJsonCleaner(key, value);
}
}
}
});
}
recursiveJsonCleaner('', jsonDummy);
// after cleaning json
console.info(JSON.stringify(jsonDummy));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment