You can use a JSON replacer to switch keys before writing.
JSON.stringify(myVal, function (key, value) {
if (value && typeof value === 'object') {
var replacement = {};
for (var k in value) {
if (Object.hasOwnProperty.call(value, k)) {
replacement[k && k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
}
}
return replacement;
}
return value;
});
For the opposite, you can use a JSON reviver.
JSON.parse(text, function (key, value) {
if (value && typeof value === 'object')
for (var k in value) {
if (/^[A-Z]/.test(k) && Object.hasOwnProperty.call(value, k)) {
value[k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
delete value[k];
}
}
return value;
});
The second optional argument is a function that is called with every value created as part of the parsing or every value about to be written. These implementations simply iterate over keys and lower-cases the first letter of any that have an upper-case letter.
There is documentation for replacers and revivers at http://json.org/js.html :
Answer from Mike Samuel on Stack OverflowThe optional reviver parameter is a function that will be called for every key and value at every level of the final result. Each value will be replaced by the result of the reviver function. This can be used to reform generic objects into instances of pseudoclasses, or to transform date strings into Date objects.
The stringifier method can take an optional replacer function. It will be called after the toJSON method (if there is one) on each of the values in the structure. It will be passed each key and value as parameters, and this will be bound to object holding the key. The value returned will be stringified.
You can use JSON.stringify(), JSON.parse(), typeof
var data = {
id: 0,
name: "SAMPLe",
forms: {
formId: 0,
id: 0,
text: "Sample Text"
}
};
var res = JSON.parse(JSON.stringify(data, function(a, b) {
return typeof b === "string" ? b.toLowerCase() : b
}));
console.log(res)
You need to recurse through the object:
https://jsbin.com/lugokek/1/edit?js,console
var x = { id: 0, name: "SAMPLe", forms: { formId: 0, id: 0, text: "Sample Text" }};
function lower(obj) {
for (var prop in obj) {
if (typeof obj[prop] === 'string') {
obj[prop] = obj[prop].toLowerCase();
}
if (typeof obj[prop] === 'object') {
lower(obj[prop]);
}
}
return obj;
}
console.log(lower(x));
How to serialize properties to lower case using System.Text.Json.JsonSerializer
Slim Build - json.stringify is not a function (lowercase issue)
jquery - How to transform all JSON keys to lowercase in javascript? - Stack Overflow
SerializeJSON with lowercase keys and override JSONConverter.java - language - Lucee Dev
What does JSON stringify do to text?
When would I need to stringify text for JSON?
What is the difference between JSON.stringify() and this tool?
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;
};
JsFiddle
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);
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.eachString.toLowerCase
So this is a pretty strange question, but the default formatting for json is something like this:
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}Where the first letter of a new word in a String for the key value is Capitalized.
What I would like to do is get the key values all lowercased so that it looks like this:
{"firstname":"John", "lastname":"Doe"},
{"firstname":"Anna", "lastname":"Smith"},
{"firstname":"Peter", "lastname":"Jones"}If anyone is wondering why it's because I'm trying to load json data into amazon redshift. For some strange reason loading json values through the auto function that is built into redshift doesn't recognize key values with Uppercase characters since redshift automatically makes the column headers all lowercase. Of course I know this isn't an AWS forum so I will keep that to a minimum, but if anyone knows how I would go about lowercasing the json key values in python that would be great.