← Blog · · ConvertCaseHQ Team · 1 min read

Fixing camelCase JSON: Converting API Payloads to snake_case

Photo from Unsplash

APIs often mix conventions: JavaScript clients produce camelCase, Python/Ruby backends expect snake_case. A thin transform layer fixes this at the serialization boundary.

function toSnakeCase(obj) {
  if (Array.isArray(obj)) return obj.map(toSnakeCase);
  if (obj && typeof obj === "object") {
    return Object.fromEntries(
      Object.entries(obj).map(([k, v]) => [
        k.replace(/[A-Z]/g, c => "_" + c.toLowerCase()),
        toSnakeCase(v),
      ])
    );
  }
  return obj;
}

© 2026 ConvertCaseHQ. All rights reserved.