How to transform all JSON keys to lowercase in javascript?
19,423
Solution 1
Try the below code
function ConvertKeysToLowerCase(obj) {
var output = {};
for (i in obj) {
if (Object.prototype.toString.apply(obj[i]) === '[object Object]') {
output[i.toLowerCase()] = ConvertKeysToLowerCase(obj[i]);
}else if(Object.prototype.toString.apply(obj[i]) === '[object Array]'){
output[i.toLowerCase()]=[];
output[i.toLowerCase()].push(ConvertKeysToLowerCase(obj[i][0]));
} else {
output[i.toLowerCase()] = obj[i];
}
}
return output;
};
Solution 2
you can have a reference from @Christophe's Answer
If you can't understand here is the code for you : link
js:
var obj = {
"Collections": {
"conTainer": {
"rowSet": [{
"containerIsArchived": "Null",
"containerOrderNo": "26",
"versionNum": "0",
"containerGlobalUniqueId": "Null",
"containerIsTenantBased": "true",
"containerCreatedBy": "user",
"containerIsDeleted": "false",
"containerTenantId": "292FEC76-5F1C-486F-85A5-09D88096F098",
"containerLayoutId": "4e13dfcd-cd3b-4a29-81bd-0f73cf9577cf",
"containerApplicationId": "0000000-0000-0000-0000-000000000000",
"containerIsActive": "Null",
"containerHeaderText": "apitest19feb16",
"containerId": "3745b273-c48d-4c94-b576-3d7aac2f7ac6",
"containerCreatedUTCDate": "2016-02-19 17:57:51.0"
}]
}
}
};
var json = JSON.stringify(obj);
var newJson = json.replace(/"([\w]+)":/g, function($0, $1) {
return ('"' + $1.toLowerCase() + '":');
});
var newObj = JSON.parse(newJson);
console.debug(newObj);
Solution 3
Here is an idea, I hope it's useful.
var obj = {
"containerIsArchived": "Null",
"containerOrderNo": "26",
"versionNum": "0",
"containerGlobalUniqueId": "Null",
"containerIsTenantBased": "true",
"containerCreatedBy": "user",
"containerIsDeleted": "false",
"containerTenantId": "292FEC76-5F1C-486F-85A5-09D88096F098",
"containerLayoutId": "4e13dfcd-cd3b-4a29-81bd-0f73cf9577cf",
"containerApplicationId": "0000000-0000-0000-0000-000000000000",
"containerIsActive": "Null",
"containerHeaderText": "apitest19feb16",
"containerId": "3745b273-c48d-4c94-b576-3d7aac2f7ac6",
"containerCreatedUTCDate": "2016-02-19 17:57:51.0"
};
for(var i in obj){
obj[i.toLowerCase()] = obj[i];
delete obj[i];
}
console.log(obj);
The last statement prints the object with all keys in lowercase. The idea is to create a new key -- the lowercase version of any given key --, then get rid of the old one.
Related videos on Youtube
Author by
bagya
Updated on June 04, 2022Comments
-
bagya almost 2 years
Actually, my requirement is to convert whole JSON(only keys) into lowercase. I tried it's converting only first key of that JSON and it's not converting whole all key. Please have a look the fiddle link or please provide any other ways to do this.
Thanks...
var obj = { "Collections": { "conTainer": { "rowSet": [{ "containerIsArchived": "Null", "containerOrderNo": "26", "versionNum": "0", "containerGlobalUniqueId": "Null", "containerIsTenantBased": "true", "containerCreatedBy": "user", "containerIsDeleted": "false", "containerTenantId": "292FEC76-5F1C-486F-85A5-09D88096F098", "containerLayoutId": "4e13dfcd-cd3b-4a29-81bd-0f73cf9577cf", "containerApplicationId": "0000000-0000-0000-0000-000000000000", "containerIsActive": "Null", "containerHeaderText": "apitest19feb16", "containerId": "3745b273-c48d-4c94-b576-3d7aac2f7ac6", "containerCreatedUTCDate": "2016-02-19 17:57:51.0" }] } } }; convertKeysToCamelCase(obj); function convertKeysToCamelCase(obj) { if (!obj || typeof obj !== "object") return null; if (obj instanceof Array) { return $.map(obj, function(value) { return convertKeysToCamelCase(value); }); } var newObj = {}; $.each(obj, function(key, value) { key = key.charAt(0).toLowerCase() + key.slice(1); newObj[key] = value; }); console.log(newObj); return newObj; };
Here the Fiddle link: fiddle
-
Luca about 8 yearsplease add the relevant code to your question, not just a fiddle link
-
Quentin about 8 yearsDid you get an error message telling you to put the code in the question instead of just linking to JS Fiddle? You need to put the code in the question and not just say that a link to JS Fiddle is code.
-
Dipali Vasani about 8 yearsstackoverflow.com/a/14697508/1982680 check this out
-
bagya about 8 years@DipaliVasani ..In my case JSON differ from that. it has object inside array.
-
Dipali Vasani about 8 yearsjsfiddle.net/dipali_vasani/bHz7x/67 check this out
-
-
Mario Chueca about 8 yearsWould only change toLowerCase instead of toUpperCase, to respond to the question, and change the name of the function ConvertKeysToLowerCase to be truthful
-
bagya about 8 yearsYeah. I already changed. Thank you very much
-
bagya about 8 years@MarioChueca - Actually one glitch is there in our coding. It's converting everything to object. I need an actual JSON format as it is with key lower case. here the problem is rowset is an array that is changing to object instead of array of object.