Created
March 19, 2014 14:57
-
-
Save GonchuB/9643473 to your computer and use it in GitHub Desktop.
convert json keys to lowercase recursively.
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
/** | |
* 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); | |
} |
Thank you very much. It's help me.
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
Thank you