Skip to content

Instantly share code, notes, and snippets.

@GonchuB
Created March 19, 2014 14:57
Show Gist options
  • Save GonchuB/9643473 to your computer and use it in GitHub Desktop.
Save GonchuB/9643473 to your computer and use it in GitHub Desktop.
convert json keys to lowercase recursively.
/**
* Created by gonchub on 19/03/14.
*/
function keysToLowerCase(obj) {
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);
}
@zbee
Copy link

zbee commented Dec 17, 2015

Thank you

@BaoXiaolu
Copy link

Thank you very much. It's help me.

@pidompa
Copy link

pidompa commented Apr 12, 2017

You should prepend the following code in order to handle arrays in object

if(obj instanceof Array) {
    for (var i in obj) {
        obj[i] = keysToLowerCase(obj[i]);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment