JavaScript object-key guide

Convert Object Keys to camelCase in JavaScript

When an API returns snake_case JSON but application code uses camelCase, convert object keys once at the boundary instead of renaming every property manually.

By Camel Case Converter Editorial TeamPublished Sep 12, 2026Last reviewed

JavaScript implementation

function toCamelKey(key) {
  return key.replace(/[-_]+([a-zA-Z0-9])/g, (_, char) => char.toUpperCase()).replace(/^([A-Z])/, (char) => char.toLowerCase())
}

function keysToCamel(value) {
  if (Array.isArray(value)) return value.map(keysToCamel)
  if (value === null || typeof value !== "object") return value
  const output = {}
  for (const [key, child] of Object.entries(value)) {
    const nextKey = toCamelKey(key)
    if (Object.hasOwn(output, nextKey)) throw new Error(`Key collision: ${nextKey}`)
    output[nextKey] = keysToCamel(child)
  }
  return output
}

Handle arrays before generic objects so objects inside arrays are transformed while scalar values remain unchanged.

Collision checks matter because two different source keys can normalize to the same camelCase name.

Production edge cases

  • Acronyms such as API, URL, UUID, and OAuth may need project-specific rules.
  • Semantic prefixes such as _id, __typename, and $schema may need to be preserved.
  • Deep and top-level-only conversion are different API contracts.
  • Class instances, Date, Map, Set, and other non-JSON values need separate handling.

Test object keys online

Paste a JSON object to test recursive key conversion, top-level-only mode, excluded keys, underscore/dollar prefixes, and collision handling before you copy the logic into an application.

Drop a .json file here or choose one from your device

Files are read only in your browser. Maximum file size: 5 MB.

Converted JSON
{
  "userProfile": {
    "firstName": "Ada",
    "createdAt": "2026-09-12"
  }
}

Parsing, file reading, formatting, and key conversion run locally in your browser. JSON input is not sent to a conversion server.