snake_case to camelCase — what a generator renames, and why
Your JSON keys and your language's naming rules disagree. Here is what gets renamed, what keeps the original name, and where it goes wrong.
Most JSON uses snake_case. Most Java, Kotlin, Swift and TypeScript use
camelCase. Go uses PascalCase for anything exported. C# uses PascalCase
for everything. So a generator has to rename almost every field, and it must
keep a record of the original name or the code cannot read the JSON at all.
What the rename looks like
Take this:
{
"user_id": 1,
"first_name": "Ada",
"HTTPStatus": 200,
"2fa_enabled": true,
"class": "premium"
}
Open this example in the tool →
| Language | user_id becomes |
How the original is kept |
|---|---|---|
| Go | UserID |
A json:"user_id" tag |
| Java | userID |
@JsonProperty("user_id") |
| C# | UserID |
[JsonPropertyName("user_id")] |
| Swift | userID |
A CodingKeys enum |
| Rust | user_id |
No rename needed |
| Python | user_id |
No rename needed |
| TypeScript | user_id |
No rename needed, unless you ask |
Rust and Python are at peace with snake_case, so nothing happens. TypeScript can hold any key, so the generator leaves it alone unless you turn on Tidy the names.
The two fields that are not simple
2fa_enabled cannot be a name in most languages, because a name may not start
with a digit. Generators put something in front: the2FaEnabled,
_2faEnabled. Ugly, but legal, and the tag keeps the real key.
class is a reserved word in Java, C#, Python, C++ and more. The generator
renames it too — classField, theClass, class_ — and keeps the tag. If
you rename it again by hand and forget the tag, the field silently stops
being read.
Acronyms are a real choice
HTTPStatus, HttpStatus, httpStatus. All three are in use, and large code
bases argue about it. The generator gives you an Acronym style option:
pascal—HTTPStatusstaysHTTPStatuscamel— becomesHttpStatusoriginal— keeps whatever your JSON hadlowerCase—httpstatus
Pick the one your project already uses. Go code usually wants ID and URL
and HTTP in capitals, because the standard library does that. Java usually
wants HttpStatus.
There is no right answer. There is only the answer that matches the file next to it.
The rule that saves you
Never edit a generated name without checking the tag. The tag, the
@JsonProperty, the CodingKeys entry — that is the only thing joining your
field to the JSON. Rename the field and keep the tag, and everything works.
Rename the field and delete the tag, and the field is quietly always empty.
This is the single most common fault with generated models, and it never raises an error. The field just has the zero value, forever, and you look for the fault in the API.
When two keys collide
{ "user_id": 1, "userId": 2, "USER_ID": 3 }
Open this example in the tool →
All three become the same name in most languages. A generator will number
them — userID, userID2, userID3 — and the tags keep them apart. The code
works, but nobody can read it. If you own the API, do not do this.
A note on the top type
The one name the generator cannot guess is the name of the thing itself. Your
JSON does not say whether it is a User, a Customer or a Profile. That is
why the tool asks you for the top type name, and why it is worth filling in:
every nested type is named after the key that holds it, so a good top name
makes the whole file read well.
Try it
Paste the five-field sample above and switch between Go, Java and Swift. Watch the same five keys become three different sets of names, each with its own way of remembering where it came from.
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to CodeRead next
When one field holds two types
An array of mixed things, a field that is a string on Monday and a number on Tuesday. What a generator does, and what you should do instead.
Which JSON fields are optional? What a generator can and cannot know
A sample shows you what was there once. It cannot show you what is always there. Here is how to tell the difference, and what to do about it.
null in JSON — missing, empty, or unknown?
A key that is absent, a key that holds null, and a key that holds an empty string are three different things. Most bugs here come from treating them as one.