JSON Object keys to different Case Style: Camel & Snake in Javascript

Prateek Srivastavak
2 min readFeb 4, 2021

--

While working on one of the projects, I faced a major issue raised by the frontend developers. They always want JSON objects key in camelCase.

But the problem doesn’t end here. Backend developers want JSON objects keys in snake_case.

Let us understand the basics.

1. Camel Case

camelCase

How do we write a JSON object using camelCase?

camelCase JSON Object Key

2. Snake Case

Snake Case in Javascript

How do we write a JSON object using snake_case?

snake_case JSON Object Key

Problem Statement -: How to convert the keys of JSON Object from camelCase to snake_case and vice-versa?

In Javascript, there are different ways to do the conversion. This is the way I have done it using lodash(Lodash Library for Javascript).

  1. camelCase to snake_case

_.mapKeys: This method creates an object with the same values as object and keys generated by running each own enumerable string keyed property of object thru iteratee. The iteratee is invoked with three arguments: (value, key, object).

2. snake_case to camelCase

Resources

  1. https://www.computerhope.com/jargon/c/camelcase.htm
  2. https://lodash.com/docs/4.17.15
  3. https://www.tutorialspoint.com/casting-a-string-to-snake-case-javascript#:~:text=JavascriptWeb%20DevelopmentFront%20End,of%20each%20word%20to%20lowercase.

--

--

Prateek Srivastavak
Prateek Srivastavak

Written by Prateek Srivastavak

Web Developer, LiveToLove, foodie, fun, technology lover, travel