I am trying to make json key to lowercase so I got a solution where it making all the json key toLowercase but I don't want to make all the json key to lower case I only want to make the second level of json key to the lowercase
so while doing console.log(data)
I am getting this output
{
'valueToBeAssign': {
titleAsName: 'random title',
urlOrWebsite: 'www.google.com',
name: 'english',
number: 123
}
}
{
'computerAssignTask': {
taskName: 'debug every thing',
issueGenerated: 'At low level',
assigneName: 'Mick',
reporterName: 'Pete',
issueResolve: 'not yet'
}
}
so I wrote a code
let json = JSON.stringify(data);
var newJson = json.replace(/"([\w]+)":/g, function ($0, $1) {
return '"' + $1.toLowerCase() + '":';
});
var newObj = JSON.parse(newJson);
console.log(newObj);
but by this I am getting all the key in lowercase
{
'valuetobeassign': {
titleasname: 'random title',
urlorwebsite: 'www.google.com',
name: 'english',
number: 123
}
}
{
'computerassigntask': {
taskname: 'debug every thing',
issuegenerated: 'At low level',
assignename: 'Mick',
reportername: 'Pete',
issueresolve: 'not yet'
}
}
what I actual want is the first key should not be changed but after that it should be changed like this
{
'valueToBeAssign': {
titleasname: 'random title',
urloOrwebsite: 'www.google.com',
name: 'english',
number: 123
}
}
{
'computerAssignTask': {
taskname: 'debug every thing',
issuegenerated: 'At low level',
assignename: 'Mick',
reportername: 'Pete',
issueresolve: 'not yet'
}
}
I want it in this format
It might be better to iterate through all the keys of the object. That way, you can discriminately alter the keys by level.
const data = {
'valueToBeAssign': {
titleAsName: 'random title',
urlOrWebsite: 'www.google.com',
name: 'english',
number: 123
}
}
const newData = {}
for (const key of Object.keys(data)) {
newData[key] = {}
for (const innerKey of Object.keys(data[key])) {
newData[key][innerKey.toLowerCase()] = data[key][innerKey]
}
}
console.log(newData)