Is there a standard on JSON naming?
I see most examples using all lower case separated by underscore, aka snake_case
, but can it be used PascalCase
or camelCase
as well?
This question is related to
json
camelcasing
pascalcasing
snakecasing
I think that there isn't a official naming convention to JSON, but you can follow some industry leaders to see how it is working.
Google, which is one of the biggest IT company of the world, has a JSON style guide: https://google.github.io/styleguide/jsoncstyleguide.xml
Taking advantage, you can find other styles guide, which Google defines, here: https://github.com/google/styleguide
Notably for me on NodeJS, if I'm working with databases and my field names are underscore separated, I also use them in the struct keys.
This is because db fields have a lot of acronyms/abbreviations so something like appSNSInterfaceRRTest looks a bit messy but app_sns_interface_rr_test is nicer.
In Javascript variables are all camelCase and class names (constructors) are ProperCase, so you'd see something like
var devTask = {
task_id: 120,
store_id: 2118,
task_name: 'generalLedger'
};
or
generalLedgerTask = new GeneralLedgerTask( devTask );
And of course in JSON keys/strings are wrapped in double quotes, but then you just use the JSON.stringify and pass in JS objects, so don't need to worry about that.
I struggled with this a bit until I found this happy medium between JSON and JS naming conventions.
Seems that there's enough variation that people go out of their way to allow conversion from all conventions to others: http://www.cowtowncoder.com/blog/archives/cat_json.html
Notably, the mentioned Jackson JSON parser prefers bean_naming
.
There is no standard naming of keys in JSON. According to the Objects section of the spec:
The JSON syntax does not impose any restrictions on the strings used as names,...
Which means camelCase or snake_case should work fine.
Imposing a JSON naming convention is very confusing. However, this can easily be figured out if you break it down into components.
Programming language for generating JSON
JSON itself has no standard naming of keys
Programming language for parsing JSON
snake_case will still make sense for those with Java entries because the existing JSON libraries for Java are using only methods to access the keys instead of using the standard dot.syntax. This means that it wouldn't hurt that much for Java to access the snake_cased keys in comparison to the other programming language which can do the dot.syntax.
Example for Java's org.json
package
JsonObject.getString("snake_cased_key")
Example for Java's com.google.gson
package
JsonElement.getAsString("snake_cased_key")
Choosing the right JSON naming convention for your JSON implementation depends on your technology stack. There are cases where one can use snake_case, camelCase, or any other naming convention.
Another thing to consider is the weight to be put on the JSON-generator vs the JSON-parser and/or the front-end JavaScript. In general, more weight should be put on the JSON-generator side rather than the JSON-parser side. This is because business logic usually resides on the JSON-generator side.
Also, if the JSON-parser side is unknown then you can declare what ever can work for you.
As others have stated there is no standard so you should choose one yourself. Here are a couple of things to consider when doing so:
If you are using JavaScript to consume JSON then using the same naming convention for properties in both will provide visual consistency and possibly some opportunities for cleaner code re-use.
A small reason to avoid kebab-case is that the hyphens may clash visually with -
characters that appear in values.
{
"bank-balance": -10
}
In this document Google JSON Style Guide (recommendations for building JSON APIs at Google),
It recommends that:
Property names must be camelCased, ASCII strings.
The first character must be a letter, an underscore (_) or a dollar sign ($).
Example:
{
"thisPropertyIsAnIdentifier": "identifier value"
}
My team follows this convention.
Source: Stackoverflow.com