Last active
June 26, 2016 20:18
-
-
Save antic183/f7d68f00f1c36e319ca17ab1ce21e1d2 to your computer and use it in GitHub Desktop.
clean json (remove undefined nodes)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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