Quick revision
The short answer
We annotate the field with @SerializedName(value = "first_name", alternate = {"fname"}). The value is the name Gson writes and the preferred name it reads, while alternate names are accepted only when reading, so old clients sending fname still work and every response uses first_name. For many fields, setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES) converts all names at once, with @SerializedName for exceptions, and a custom FieldNamingStrategy covers other naming rules; if one JSON object contains both names, the last one read wins.
What to remember
In this answer, we saw that @SerializedName(value = "first_name", alternate = {"fname"}) makes Gson write first_name and accept both first_name and fname, while the Java name itself is no longer accepted unless listed. If both names arrive together, the last one read wins. For whole classes, setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES) converts every name, a custom FieldNamingStrategy covers other conventions, and @SerializedName handles the exceptions in both cases.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We annotate the field with @SerializedName(value = "first_name", alternate = {"fname"}). The value is the name Gson writes and the preferred name it reads, while alternate names are accepted only when reading, so old clients sending fname still work and every response uses first_name. For many fields, setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES) converts all names at once, with @SerializedName for exceptions, and a custom FieldNamingStrategy covers other naming rules; if one JSON object contains both names, the last one read wins.
Why this question is asked
Java code uses camelCase, many APIs use snake_case, and old clients rarely disappear on schedule. Every long-lived API eventually has to accept a name it no longer writes. Gson handles this neatly, but only if we know the difference between the name it writes and the names it accepts. Interviewers ask this coding question because the alternate attribute is easy to forget and its behaviour has edge cases. In this answer, we will recap how Gson chooses JSON names, handle first_name and fname on one field, see what happens when both arrive together, and then look at policies and strategies for whole classes.
1A Quick Recap of How Gson Names Properties
By default, Gson uses the Java field name as the JSON key, unchanged. The field firstName becomes "firstName". Two things can change that. A field naming policy, set on the builder, transforms every field name with a rule. @SerializedName on a single field overrides the policy for that field and gives an exact name. With neither, the JSON must match the Java name exactly, so our payload {"first_name":"Ana"} leaves firstName null, silently, because Gson ignores unknown properties.
2Handling Both Names on One Field
Here is the class with the annotation, and what it does in each direction.
| JSON key | Read? | Written? |
|---|---|---|
| first_name (value) | yes | yes, always |
| fname (alternate) | yes | never |
firstName (Java name) | no, unless listed as alternate | never |
Notice the asymmetry. The value attribute, first_name, is used for writing and reading. The alternate names are only ever used for reading, so a response never contains fname. That is exactly the behaviour we want for a migration: old clients can keep sending the old name while every client receives the new one.
Notice also the third case. Once @SerializedName is present, the Java field name itself is no longer an accepted JSON name. If some clients send firstName, it must be listed among the alternates too.
3When Both Names Appear in One Object
A confused client may send both names. Gson does not treat this as an error. It reads the object's properties in order and sets the field each time it meets one of its names, so the last one read wins.
This is rarely what anyone wants, but it is predictable. If such payloads are possible, the safest response is to log or reject them in a custom adapter, or document that clients must send only one name. It is worth a test, so a future Gson upgrade that changes the behaviour does not go unnoticed.
4Converting All Fields With a Naming Policy
If the whole API uses snake_case, annotating every field is tedious. A naming policy converts every field name with a rule, and @SerializedName handles the exceptions.
The annotation takes priority over the policy, so firstName keeps its alternate while every other field is converted by the rule. The policy does not produce alternates on its own: it only defines the one name per field, so fname still needs the annotation. Other built-in policies include UPPER_CAMEL_CASE (FirstName) and LOWER_CASE_WITH_DASHES (first-name).
5Custom Rules With FieldNamingStrategy
When no built-in policy fits, FieldNamingStrategy is a one-method interface that receives the java.lang.reflect.Field and returns the JSON name. It is useful for company conventions, such as a prefix or a mapping table from an older system.
@SerializedName still takes priority over the strategy, so the same pattern applies: one rule for the class, explicit annotations for the exceptions.
6Follow-up questions
Can we accept several old names?
Yes. alternate is an array, so @SerializedName(value = "first_name", alternate = {"fname", "firstName", "given_name"}) accepts all of them on input while still writing only first_name.
How would we retire fname eventually?
We log or count requests that use the alternate name, for example in a custom adapter or at the API gateway. When usage drops to zero, or after an announced date, we remove it from the alternate list in a release note.
Does @SerializedName work with records?
Yes, in Gson versions with record support, 2.10 and later, the annotation on a record component is honoured in the same way. Check your version.
7Conclusion
In this answer, we saw that @SerializedName(value = "first_name", alternate = {"fname"}) makes Gson write first_name and accept both first_name and fname, while the Java name itself is no longer accepted unless listed. If both names arrive together, the last one read wins. For whole classes, setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES) converts every name, a custom FieldNamingStrategy covers other conventions, and @SerializedName handles the exceptions in both cases.
Open with
I would put @SerializedName on the field with value first_name and alternate fname: @SerializedName(value = "first_name", alternate = {"fname"}) private String firstName. The value is the name Gson writes and also reads, while the alternates are only accepted when reading.
Then, if they want more
So old clients sending fname keep working, and every response contains first_name only, which is what we want during a migration. One detail is that once the annotation is there, the Java name firstName is no longer accepted, so if any client sends that, I add it to the alternates too.
If a payload contains both first_name and fname, Gson does not complain; it sets the field each time, so whichever appears last wins, and I would cover that with a test or reject it if it matters.
If the whole API is snake_case, I would rather set the naming policy on the shared builder, FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES, and use @SerializedName only for exceptions like this alternate name, because the policy produces one name per field and no alternates. For other conventions, a custom FieldNamingStrategy, which receives the Field and returns the name, does the job.
Later I would track usage of fname and remove it once old clients are gone.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "The JSON uses first_name while the Java field is firstName, and older clients send...": https://scrivio-interview-library.netlify.app/java/json-libraries/feb8f9035d/