Code Review Videos > How I Fixed > Convert JSON Keys from snake_case to camelCase in IntelliJ IDEs

Convert JSON Keys from snake_case to camelCase in IntelliJ IDEs

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.

webstorm ide export sql as json

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:

webstorm export sql as json preview

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 to firstName
  • last_name to lastName
  • email_address to emailAddress

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:

jetbrains install camelcase plugin

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:

re-arrange the camel case plugin option order

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,

webstorm regex match on json keys

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:

webstorm select all regex matching filter

And the last step is to toggle to camelCase:

Toggle to camel case keys for JSON in intellij ide

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:

converted camel case json in intellij ide

Perfecto.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.