JSON Object keys to different Case Style: Camel & Snake in Javascript
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
How do we write a JSON object using camelCase?
2. Snake Case
How do we write a JSON object using snake_case?
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).
- 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