Convert returned JSON Object Properties to (lower first) camelCase

I have JSON returned from an API like so:

Contacts: [{ GivenName: "Matt", FamilyName: "Berry" }]

To keep this consistent with my code style (camelCase – lower case first letter) I want to transform the array to produce the following:

 contacts: [{ givenName: "Matt", familyName: "Berry" }]

What’s the easiest/best way to do this? Create a new Contact object and iterate over all the contacts in the returned array?

var jsonContacts = json["Contacts"],
    contacts= [];
        
_.each(jsonContacts , function(item){
    var contact = new Contact( item.GivenName, item.FamilyName );
    contacts.push(contact);
});

or can I map the original array or transform it somehow?

32 thoughts on “Convert returned JSON Object Properties to (lower first) camelCase”

Leave a Comment