In this post I will cover a way to convert only JSON keys from snake_case
format to camelCase
format, without impacting the values.
I hit on this problem today, and found multiple solutions that rely on code to solve this problem. I didn’t need a code solution, as my use case was something of a one time thing. I needed to convert a test from Groovy to Cypress. That test had a ton (read: 50+) database setup calls. After a lot of hair pulling (I don’t have much hair anyway), I figured dumping the SQL that Groovy creates to JSON, and then directly using that was the best idea.
This is actually really easy in any Jetbrains IDE.
When you select JSON
as the export type, and then click the export icon (it’s the one right next to the word CSV on the top bar above), you should see a little preview of what you’re about to get:
And you can then either export to a file, or copy the data to your clipboard. Note, it only shows 10 records, so if you have more then it will still copy them all to your clipboard. That caught me out at first. Silly, I know.
This should then give you the JSON:
[
{
"id": 1,
"first_name": "John",
"last_name": "Doe",
"email_address": "johndoe@example.com",
"phone_number": "555-555-1212",
"date_of_birth": "1990-01-01",
"street_address": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip_code": "12345"
},
{
"id": 2,
"first_name": "Jane",
"last_name": "Doe",
"email_address": "janedoe@example.com",
"phone_number": "555-555-1212",
"date_of_birth": "1991-02-02",
"street_address": "456 Elm St",
"city": "Otherville",
"state": "NY",
"zip_code": "67890"
},
// ... etc
]
Code language: JSON / JSON with Comments (json)
What we want now is to convert:
first_name
tofirstName
last_name
tolastName
email_address
toemailAddress
And so on.
It should not, in any way, change city
or state
for example. Nor should it change any values, such as “John” or “123 Main St”.
My Fix
The way I ‘fixed’ this was with a combination of a regex, and an IntelliJ plugin. This plugin works in all the IntelliJ IDEs – so WebStorm, PHPStorm, Rider, and IntelliJ etc.
Start by installing the plugin. No restart is required, thankfully:
This is a free plugin.
Note in the Overview section it gives some notes about being able to swap between kebab, snake, pascal, camel, and more. Pretty neat.
However, by default the ordering it swaps between is … funky.
You can use the keyboard shortcut to toggle between selections, but an easier way is to simply change the ordering in the menu:
Once you’ve installed the plugin, search for “camel” in the top left of your Settings menu. This should, very helpfully, narrow down the menu options to anything containing the word “camel”.
You need the Camel Case plugin, and then change the order as desired – probably putting camelCase
at the top, if you’re following this post. Then Apply > OK out of that window.
Next, you need to select all the JSON keys.
Here’s the regex you need:
\b\w+(?=":\s)
This regex will match any word characters that appear just before the colon and whitespace, and are enclosed in speech marks. The “\b” at the beginning of the regex ensures that only whole words are matched.
A variant on this is:
\b[a-z]+(?:_[a-z]+)+\b:
Code language: CSS (css)
This one is for if your IDE has “helpfully” already removed the speech marks for you.
Open up the “find” menu with ctrl or command + F. You can do that via the “Edit” menu also.
Be sure to tick the RegEx icon,
At this point your IDE has highlighted all the matches, but it’s not selected them.
In order to select all the matches, use the little three dots icon:
And the last step is to toggle to camelCase
:
Depending on what ordering you used (as above) for Camel Case plugin, you may need to repeat this step multiple times to get the desired outcome. Hence why we re-ordered.
Or, of course, you can use the keyboard shortcut.
However you get there, you should now get the camelCasedKeys you desired:
Perfecto.