How to parse JSON to object with lower case key
That is function:
function JSON_Lower_keys(J) {
var ret={};
$.map(JSON.parse(J),function(value,key){
ret[key.toLowerCase()]=value;
})
return ret;
}
that is call:
console.log(JSON_Lower_keys('{"ID":1234, "CONTENT":"HELLO"}'))
How about this:
json.replace(/"([^"]+)":/g,function($0,$1){return ('"'+$1.toLowerCase()+'":');}));
The regex captures the key name $1 and converts it to lower case.
Live demo: http://jsfiddle.net/bHz7x/1/
[edit] To address @FabrícioMatté's comment, another demo that only matches word characters: http://jsfiddle.net/bHz7x/4/
Iterate over the properties and create lowercase properties while deleting old upper case ones:
var str = '{"ID":1234, "CONTENT":"HELLO"}';
var obj = $.parseJSON(str);
$.each(obj, function(i, v) {
obj[i.toLowerCase()] = v;
delete obj[i];
});
console.log(obj);
//{id: 1234, content: "HELLO"}
Fiddle
Or you can just build a new object from the old one's properties:
var obj = $.parseJSON(str),
lowerCased = {};
$.each(obj, function(i, v) {
lowerCased[i.toLowerCase()] = v;
});
Fiddle
References:
jQuery.each
String.toLowerCase