For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/object/toSnakeCaseKeys.md.

toSnakeCaseKeys es-toolkit

Returns a new object with all keys in objects and arrays converted to snake_case.

Snake case is a naming convention where each word in an identifier is written in lowercase and connected with underscores (_). For example, it's written as snake_case.

const snakeCased = toSnakeCaseKeys(obj);

Usage

toSnakeCaseKeys(obj)

Use toSnakeCaseKeys when you want to convert all keys of an object to snake_case. Nested objects and objects within arrays are also converted recursively.

For example, object keys are converted as follows:

  • camelCasesnake_case (e.g. userIduser_id)
  • PascalCasesnake_case (e.g. UserIduser_id)
  • UPPERCASE_KEYSsnake_case (e.g. FIRST_NAMEfirst_name, LASTlast)
import { toSnakeCaseKeys } from 'es-toolkit/object';

// Basic object conversion
const obj = { userId: 1, firstName: 'John', lastName: 'Doe' };
const result = toSnakeCaseKeys(obj);
// result is { user_id: 1, first_name: 'John', last_name: 'Doe' }

// Objects within arrays are also converted
const users = [
  { userId: 1, firstName: 'John' },
  { userId: 2, firstName: 'Jane' },
];
const convertedUsers = toSnakeCaseKeys(users);
// convertedUsers is [{ user_id: 1, first_name: 'John' }, { user_id: 2, first_name: 'Jane' }]

// Nested objects are fully converted
const nested = {
  userData: {
    userId: 1,
    contactInfo: {
      emailAddress: '[email protected]',
      phoneNumber: '123-456-7890',
    },
  },
};
const nestedResult = toSnakeCaseKeys(nested);
// nestedResult is {
//   user_data: {
//     user_id: 1,
//     contact_info: {
//       email_address: '[email protected]',
//       phone_number: '123-456-7890'
//     }
//   }
// }

Parameters

objTrequired

The object, array, or primitive value to convert keys to snake_case.

Returns

Returns a new object with all keys converted to snake_case.

ToSnakeCaseKeys<T>
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.