How to Convert snake_case to camelCase in JavaScript
4 min read
Converting snake_case API responses to camelCase is one of the most common frontend tasks.
Simple String Conversion
function snakeToCamel(str) {
return str.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
}
snakeToCamel("user_first_name"); // "userFirstName"
Convert All JSON Keys
function camelizeKeys(obj) {
if (Array.isArray(obj)) return obj.map(camelizeKeys);
if (obj !== null && typeof obj === 'object') {
return Object.fromEntries(
Object.entries(obj).map(([k, v]) => [snakeToCamel(k), camelizeKeys(v)])
);
}
return obj;
}
const api = { user_name: "Alice", created_at: "2026-01-01" };
camelizeKeys(api); // { userName: "Alice", createdAt: "2026-01-01" }
Using Libraries
humps, camelcase-keys, and change-case are popular npm packages for this.
Online Tool
Use the camelCase converter or case converter hub.