0

I want to change a json array keys names from upper case letters to a lower case keys as the following

[
    {
        "_id": "581f2749fb9b6f22308f5063",
        "WorkshopId": "1",
        "WorkshopTitle": "workshop1",
        "WorkshopPrice": "200",
        "WorkshopDescription": "workshop1 is a test workshop",
        "FloorNumber": "1",
        "RoomNumber": "205",
        "WorkshopLanguage": "english language",
        "LastOnlineRegistrationDate": "15/10/2016",
        "WorkshopDate": "1/11/2016",
        "WorkshopStartTime": "8:00 AM",
        "WorkshopEndTime": "11:00 AM",
        "WorkshopRules": "Rules will be mentioned here",
        "WorkshopCapacity": "200",
        "Speaker": {
            "SpeakerName": "John doe",
            "AboutSpeaker": "About the speaker"
        }
    },
    {
        "_id": "581f27e796915434f44cd678",
        "WorkshopId": "2",
        "WorkshopTitle": "workshop2",
        "WorkshopPrice": "200",
        "WorkshopDescription": "workshop2 is a test workshop",
        "FloorNumber": "1",
        "RoomNumber": "205",
        "WorkshopLanguage": "english language",
        "LastOnlineRegistrationDate": "15/10/2016",
        "WorkshopDate": "1/11/2016",
        "WorkshopStartTime": "11:00 AM",
        "WorkshopEndTime": "02:00 PM",
        "WorkshopRules": "Rules will be mentioned here",
        "WorkshopCapacity": "200",
        "Speaker": {
            "SpeakerName": "Jane doe",
            "AboutSpeaker": "Jane doe - About the speaker"
        }
    }
]

for example WorkshopId must be changed to workshopid, I have a function in node js that query a collection in mongodb and return the json :

getWorkshops: function (db, response) {
                db.collection('Workshops').find().toArray(function (err, results) {
                    var convertedArr = [];
                    //convert the json.
                    response.send(JSON.stringify(convertedArr));
        });

any help?

Voice Of The Rain
  • 495
  • 1
  • 8
  • 24
  • Do you want all the key's letter lowercase or only the first one? – wscourge Nov 08 '16 at 13:20
  • 1
    Possible duplicate of [How to parse JSON to object with lower case key](http://stackoverflow.com/questions/14697352/how-to-parse-json-to-object-with-lower-case-key) – Jagrut Nov 08 '16 at 13:22

2 Answers2

1

This will map an object's keys to lowercase:

var upperCased = [
    { ID: 1, NAME: 'Fred' },
  { ID: 2, NAME: 'Sarah' },
    { ID: 3, NAME: 'Joe' },
];


var lowerCased = upperCased.map(function(item) {
  var mapped = {};
  for (var key in item) {
    mapped[key.toLowerCase()] = item[key];
  }

  return mapped;
});

https://jsfiddle.net/5ouebw4b/2/

Mark Williams
  • 2,268
  • 1
  • 11
  • 14
  • The map function will do the trick , how I can lower case the inner keys if the collection has nested collections? – Voice Of The Rain Nov 09 '16 at 08:30
  • Looks like a candidate for recursion; you'd have to extract the logic into a function that can call itself recursively to cater for several levels. – Mark Williams Nov 09 '16 at 08:54
  • Works where the nested items are arrays; if the nested item was an object you'd also have to test for that and process it, but hopefully it's good to get you going. All the best. – Mark Williams Nov 09 '16 at 09:04
0

You could to it with a custom toJSON() function:

getWorkshops: function (db, response) {
    db.collection('Workshops').find().toArray(function (err, results) {
        results.toJSON = function () {
            var newArr = [];
            for (var obj in this) {
                if (!this.hasOwnProperty(obj) || 'toJSON' === obj) continue;

                var newObj = {};
                for (var prop in this[obj]) {
                    if (!this[obj].hasOwnProperty(prop)) continue;

                    newObj[prop.toLowerCase()] = this[obj][prop];
                }

                newArr.push(newObj);
            }

            return newArr;
        };

        response.send(JSON.stringify(results));
    });
}
Hyddan
  • 1,297
  • 15
  • 24