3

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

DaBler
  • 2,695
  • 2
  • 26
  • 46
bagya
  • 383
  • 1
  • 11
  • 38

5 Answers5

8

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

Nofi
  • 2,107
  • 1
  • 15
  • 23
  • Would only change toLowerCase instead of toUpperCase, to respond to the question, and change the name of the function ConvertKeysToLowerCase to be truthful – Mario Chueca Mar 29 '16 at 09:57
  • Yeah. I already changed. Thank you very much – bagya Mar 29 '16 at 10:45
  • 2
    @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. – bagya Mar 30 '16 at 11:01
  • A warning from a time traveler from 2022: this is only handling the first item in arrays so don't just copy paste this function blindly. – Jason Sep 12 '22 at 20:45
3

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);
Community
  • 1
  • 1
Dipali Vasani
  • 2,526
  • 2
  • 16
  • 30
2

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.

mtchuente
  • 334
  • 5
  • 9
1

Modify the below answer a bit to make it easier to read

function convertKeysToCamelCase(obj) {
    var output = {}
    for (var i in obj) {
        var current = obj[i]
        if (isObject(current) || isArray(current)) {
            output[i.toLowerCase()] = convertKeysToCamelCase(current);
        } else {
            output[i.toLowerCase()] = current;
        }
    }
    return output;
};

function isObject(obj) {
    return typeof obj === 'object'
}

function isArray(arr) {
    return Array.isArray(arr)
}

console.log(JSON.stringify(convertKeysToCamelCase(input)));
Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54
0

You need to recurse into the object so that the keys at the lower level are covered. To do this, it would be better to create a copy of the object first and then modify that, like so

// create copy of object
var newObj = JSON.parse(JSON.stringify(obj));
convertKeysToCamelCase(newObj);

function convertKeysToCamelCase(obj) {
    if (!obj || typeof obj !== "object") return null;

    if (obj instanceof Array) {
        return $.map(obj, function(value) {
            return convertKeysToCamelCase(value);
        });
    }

    // manipulates the object being passed in
    $.each(obj, function(key, value) {
        // delete existing key
        delete obj[key];
        key = key.charAt(0).toLowerCase() + key.slice(1);
        obj[key] = value;
        convertKeysToCamelCase(value);
    });

    return obj;
};

Fiddle - http://jsfiddle.net/sbbnmz0b/

potatopeelings
  • 40,709
  • 7
  • 95
  • 119