Thursday, November 6, 2025
HomeHow toConvert javascript multi-level object keys into snake_case

Convert javascript multi-level object keys into snake_case

Published on

spot_img

You can use a recursive approach to convert all keys of a multi-level object to snake_case. Here’s a JavaScript function that does that:

JavaScript
function convertKeysToSnakeCase(obj) {
  if (obj && typeof obj === 'object') {
    if (Array.isArray(obj)) {
      return obj.map(item => convertKeysToSnakeCase(item));
    } else {
      const snakeCaseObj = {};
      for (const key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          let snakeCaseKey = key
            .replace(/([a-z])([A-Z])/g, '$1_$2') // Insert underscore between lowercase and uppercase letters
            .toLowerCase();

          if (/^[A-Z]/.test(key)) {
            // If the word starts with a capital letter, remove the leading underscore
            snakeCaseKey = snakeCaseKey.substring(1);
          }

          snakeCaseObj[snakeCaseKey] = convertKeysToSnakeCase(obj[key]);
        }
      }
      return snakeCaseObj;
    }
  } else {
    return obj;
  }
}


// Example usage:
const inputObject = {
  firstName: 'John',
  lastName: 'Doe',
  addressInfo: {
    streetAddress: '123 Main St',
    cityInfo: {
      cityName: 'Cityville',
      postalCode: '12345'
    }
  },
  phoneNumbers: ['123-456-7890', '987-654-3210'],
  camelCaseKey: 'HelloWorld',
  mixedCaseKey: 'someMixedCaseValue'
};

const snakeCaseObject = convertKeysToSnakeCase(inputObject);
console.log(snakeCaseObject);

This function convertKeysToSnakeCase takes an object as input and recursively converts all keys to snake_case.

Kids Learning Corner

Maths Addition Worksheets For Class 2

2 Digit Addition Worksheet Welcome to our 2-Digit Addition Worksheets page. Here you will find a...

Latest articles

Maths Addition Worksheets For Class 2

2 Digit Addition Worksheet Welcome to our 2-Digit Addition Worksheets page. Here you will find a...

Mastering AWS CDK: Essential Commands, Examples, and Resources for Cloud Infrastructure as Code

Unlock the power of AWS CDK with our comprehensive guide! Learn essential commands, explore practical examples, and discover valuable resources to streamline your cloud infrastructure development. Dive into the world of infrastructure as code and take your AWS projects to the next level.

Node.js Modules

What is a Module in Node.js? A module is a way of grouping related functions...

How to get an MySQL root user password in AWS Lightsail

I have recorded an video to help you on lightsail server. In this video...

More like this

Node.js Modules

What is a Module in Node.js? A module is a way of grouping related functions...

How to get an MySQL root user password in AWS Lightsail

I have recorded an video to help you on lightsail server. In this video...

Node.js Get Started

Download & Install Node.js Follow official Node.js guide for Node.js installation on your environment: https://nodejs.org Getting...