Skip to content

Instantly share code, notes, and snippets.

@radutta
Created May 9, 2017 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save radutta/4480e8292372da56b426f7a4c65f8774 to your computer and use it in GitHub Desktop.
Save radutta/4480e8292372da56b426f7a4c65f8774 to your computer and use it in GitHub Desktop.
Convert JSON Keys to lowercase in Javascript
function keysToLowerCase(obj) {
if(obj instanceof Array) {
for (var i in obj) {
obj[i] = keysToLowerCase(obj[i]);
}
}
if (!typeof(obj) === "object" || typeof(obj) === "string" || typeof(obj) === "number" || typeof(obj) === "boolean") {
return obj;
}
var keys = Object.keys(obj);
var n = keys.length;
var lowKey;
while (n--) {
var key = keys[n];
if (key === (lowKey = key.toLowerCase()))
continue;
obj[lowKey] = keysToLowerCase(obj[key]);
delete obj[key];
}
return (obj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment