How to Convert returned JSON Object Properties to camelCase in Lodash?
Last Updated :
01 Oct, 2024
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"
}
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.
Similar Reads
How to Convert String to Camel Case in JavaScript?
We will be given a string and we have to convert it into the camel case. In this case, the first character of the string is converted into lowercase, and other characters after space will be converted into uppercase characters. These camel case strings are used in creating a variable that has meanin
4 min read
How to Convert Object to Array in Lodash ?
Converting an Object to an Array consists of changing the data structure from key-value pairs to an array format. Below are the different approaches to converting objects to arrays in Lodash: Table of Content Using toArray function Using values functionRun the below command before running the below
2 min read
How to Remove Object Properties with Lodash?
Lodash is a powerful JavaScript utility library that makes manipulating arrays, objects, strings, and more easier. One of the common tasks when dealing with objects is removing properties, either based on specific criteria or simply for cleaning up unwanted data. Below are the following ways to remo
2 min read
How to Convert String of Objects to Array in JavaScript ?
This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i
4 min read
How to Convert Object Containing Objects into Array of Objects using Lodash?
Lodash is a JavaScript utility library that provides predefined functions to make code more readable and cleaner. These functions are optimized for performance, often being faster than native JavaScript methods for complex operations.We will learn how to convert an object containing objects into an
4 min read
How to convert hyphens to camel case in JavaScript ?
Given a string containing hyphens (-) and the task is to convert hyphens (-) into camel case of a string using JavaScript. Approach: Store the string containing hyphens into a variable.Then use the RegExp to replace the hyphens and make the first letter of words upperCase. Example 1: This example co
2 min read
How to convert camel case to snake case in JSON response implicitly using Node.js ?
Camel case and snake case are two common conventions for naming variables and properties in programming languages. In camel case, compound words are written with the first word in lowercase and the subsequent words capitalized, with no spaces or underscore between them. For example, firstName is wri
5 min read
How to Convert JavaScript Class to JSON in JavaScript?
When transmitting or saving class data in a structured manner, converting an instance of a class into a JSON format is also necessary. JSON (JavaScript Object Notation) supplies a method of turning objects into strings. They can be sent across a network or stored in a database, properties of the ins
2 min read
How to Find Property by Name in a Deep Object Using Lodash?
When working with deeply nested objects in JavaScript, finding a specific property can be challenging. Using Lodash, a powerful utility library, simplifies this task with its robust set of functions. This guide explores how to effectively search for a property by name within a deeply nested object u
2 min read
How to Convert an Object into Array of Objects in JavaScript?
Here are the different methods to convert an object into an array of objects in JavaScript 1. Using Object.values() methodObject.values() method extracts the property values of an object and returns them as an array, converting the original object into an array of objects. [GFGTABS] JavaScript const
3 min read