Open In App

How to Convert returned JSON Object Properties to camelCase in Lodash?

Last Updated : 01 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with JSON data in JavaScript, it's common to need consistent property naming conventions, such as camelCase. Lodash provides utilities that can help with this transformation.

These are the various approaches to convert object properties to camelCase using Lodash:

Using _.mapKeys() Method

We use _.mapKeys() to create an object composed of the keys generated by running each enumerable string key of the object through a given function. We can define a function to convert each key to camelCase.

Syntax:

_.mapKeys(object, iteratee);

Example: In this example, we will be using the Using _.mapKeys() and a Custom Function.

JavaScript
const _ = require('lodash'); 

// Define an object with snake_case properties
const snakeCaseObject = {
    first_name: 'Prateek',
    last_name: 'Sharma'
};

// Use mapKeys to convert keys to camelCase
const camelCaseObject = _.mapKeys(snakeCaseObject, (value, key) => {
    return _.camelCase(key); 
    // Convert each key to camelCase
});

// Output the result
console.log(camelCaseObject);

Output:

{
"firstName": "Prateek",
"lastName": "Sharma"
}

Using _.transform() Method

We use _.transform() to transform an object or array into a new structure, thus allowing us to apply custom logic during the transformation.

Syntax:

_.transform(object, iteratee, [accumulator]);

Example: In this example we will be using the Using _.transform() Method.

JavaScript
const _ = require('lodash'); 
// Import lodash library

// Define an object with snake_case properties
const snakeCaseObject = {
    first_name: 'Prateek',
    last_name: 'Sharma'
};

// Use transform to create a new object with camelCase keys
const camelCaseObject = _.transform(snakeCaseObject, 
(result, value, key) => {
    result[_.camelCase(key)] = value;
    // Convert key and assign value
}, {});

// Output the result
console.log(camelCaseObject);

Output:

{
"firstName": "Prateek",
"lastName": "Sharma"
}

Using _.reduce() Method

We use _.reduce() method to iterate over an object and accumulate results based on our custom logic, which in this case involves converting keys to camelCase.

Syntax:

_.reduce(collection, iteratee, [accumulator]);

Example: In this example we will be using the Using _.reduce() Method.

JavaScript
const _ = require('lodash');
// Import lodash library

// Define an object with snake_case properties
const snakeCaseObject = {
    first_name: 'Prateek',
    last_name: 'Sharma'
};

// Use reduce to create a new object with camelCase keys
const camelCaseObject = _.reduce(snakeCaseObject, 
(result, value, key) => {
    result[_.camelCase(key)] = value;
    // Convert key and assign value
    return result; 
    // Return the accumulated result
}, {});

// Output the result
console.log(camelCaseObject);

Output:

{
"firstName": "Prateek",
"lastName": "Sharma"
}

Using _.merge() Method with a Recursive Function

We can use _.merge()  to recursively convert properties, especially useful when dealing with nested objects.

Syntax:

_.merge(object, [sources]);

Example: In this example we will be using the Using _.merge() with a Recursive Function.

JavaScript
const _ = require('lodash');
// Import lodash library

// Define a nested object with snake_case properties
const snakeCaseObject = {
    user_info: {
        first_name: 'Prateek',
        last_name: 'Sharma'
    }
};

// Recursive function to convert keys to camelCase
function fun (obj) {
    return _.transform(obj, (result, value, key) => {
        const newKey = _.camelCase(key); 
        // Convert key to camelCase
        result[newKey] = _.isObject(value) ?
        convertKeysToCamelCase(value) : value;
        // Recursively convert if value is an object
    }, {});
}

// Convert the nested object to camelCase
const camelCaseObject = fun(snakeCaseObject);

// Output the result
console.log(camelCaseObject);

Output:

{
"userInfo": {
"firstName": "Prateek",
"lastName": "Sharma"
}
}

Conclusion

In this article, we explored multiple approaches to converting JSON object properties to camelCase using Lodash. Each method has its strengths, and the choice of which to use can depend on the specific requirements of our project, such as whether we're handling nested objects or need a straightforward conversion. With Lodash, transforming object properties becomes efficient and easy.


Next Article

Similar Reads