Should I Use camelCase or underscore_case for JSON?

A comparison of the pros and cons of using camelCase vs underscores for naming keys in JSON data.
On this page

Should I Use camelCase or underscore_case for JSON?

Excerpt

JSON supports two popular naming conventions for keys - camelCase and underscore_case. This article explores the readability, compatibility, and other factors to consider when choosing between these formats.


JSON, short for JavaScript Object Notation, has become a ubiquitous data format used in web APIs, configuration files, and data storage. When formatting JSON keys, developers have two main naming convention options: camelCase and underscore_case. This post will dive into the debate between these two styles and provide guidelines on when each one may be preferable.

Brief Background on JSON

JSON is a lightweight data interchange format that uses human-readable text to transmit data objects. It was originally based on a subset of JavaScript syntax, but has since become a language-independent standard.

Here is an example JSON object:

 1{
 2  "firstName": "John",
 3  "lastName": "Doe",
 4  "age": 35,
 5  "address": {
 6    "street": "123 Main St",
 7    "city": "Anytown",
 8    "state": "CA"
 9  }
10}

As you can see, JSON uses key-value pairs to store data. The keys in the above example use camelCase conventions. JSON is widely used for web APIs, configuration files, and data storage due to its simplicity, readability, and universal support across programming languages.

Naming Convention Options

When formatting the keys in a JSON object, there are two primary naming convention options:

camelCase: The first letter is lowercase, and each subsequent word is capitalized (no spaces between words). For example: firstName, lastName, addressInfo.

Below I will provide a free online tool to help you achieve camelCase convert:

underscore_case: Each word is separated by an underscore. For example: first_name, last_name, address_info.

Below I will provide a free online tool to help you achieve underscore_case convert:

Both conventions have their merits when used for JSON keys, which is what has fueled the debate over which style is preferable.

The Case for camelCase

There are several advantages to using camelCase naming for JSON keys:

  • Readability - camelCase can be easier to read, especially for compound names. firstName flows better than first_name.

  • Alignment with JavaScript - Since JSON has its roots in JavaScript, camelCase aligns with JavaScript naming conventions and feels natural.

  • Language support - Most JSON parsers in modern languages properly support keys in camelCase style. There’s no need to transform keys.

  • Conversion ease - It’s simpler to convert camelCase to underscore or other formats using string manipulation. Harder to split on underscores.

  • Popularity - Many widely used JSON APIs and conventions use camelCase, including Twitter, Github, JSON:API, and others.

The Case for underscore_case

Using underscores for JSON keys also has some advantages:

  • Clarity - The underscore visually separates words clearly. Easier to parse than jammed together camelCase.

  • Avoiding ambiguity - underscore avoids ambiguity for keys like clientID vs clientId that could alter meaning.

  • Readability - Some find the explicit word separator of underscore more readable than camelCase.

  • Consistency with SQL - Underscore aligns better with SQL naming conventions like first_name used in database tables.

  • Legacy systems - Older systems sometimes rely on underscore to properly parse keys.

There is no industry-wide standard here. Some examples of conventions used by popular frameworks:

  • Django - Uses underscore by default for JSON keys. Settings use camelCase.

  • Rails - Defaults to underscore but can override to camelCase via config.

  • ASP.NET - Defaults to camelCase but underscores also commonly used.

  • Express - No opinion, up to developer preference. Both used.

When to Use Each Convention

With no universal standard, the most important thing is to pick one naming convention and stick with it for consistency, at least within a given project.

However, here are some guidelines on when each style may be preferable:

Consider camelCase when:

  • Your project is JavaScript-based
  • Prioritizing readability for compound names
  • Converting between cases frequently
  • Interoperating with certain JSON APIs

Consider underscore_case when:

-Aligning with SQL database conventions

  • Avoiding ambiguity is critical
  • Working with legacy systems that require it
  • You simply prefer the visual separation

Itoolkit recommends using camelCase by default for JSON keys as it provides better readability in most cases. But underscore_case is also acceptable if applied consistently.

Tools for Automatic Case Conversion

Thankfully, there are utilities available to convert between camelCase and underscore automatically:

  • JavaScript libraries like lodash can handle case conversion
1const _ = require("lodash");
2
3_.camelCase("first_name"); // 'firstName'
4_.snakeCase("lastName"); // 'last_name'
  • Python has case conversion libraries like pyhumps

  • Editor plugins for IDEs like IntelliJ IDEA can convert cases on the fly

  • Linters like eslint can also enforce consistent casing

Conclusion

The choice between camelCase and underscore_case for formatting JSON keys has valid reasons on both sides of the debate. There is no universally agreed upon standard.

The main thing is to pick one style and stick with it consistently within a project. Both camelCase and underscore_case are reasonable options with their own tradeoffs.

For many developers, camelCase may provide better overall readability - especially for JavaScript-based systems. But underscore_case also has advantages in some situations, like aligning with SQL conventions.

The good news is that modern languages and tools make it easy to work with either naming style. So developers are able to choose the convention they prefer, while still maintaining interoperability with diverse systems.