JSON to Java: Differences, Edge Cases & Pitfalls
Generate Java POJOs from JSON with a clear split between primitives and wrappers, plus Jackson or Gson annotation notes.
The output is a plain POJO not tied to any JSON library, so snake_case keys will not map themselves — add @JsonProperty or @SerializedName. The primitive-versus-wrapper rule is clear: anything nullable or optional must use the wrapper type.
Format Differences
| Aspect | JSON | Java POJO |
|---|---|---|
| Naming | camelCase / snake_case mixed | camelCase fields with annotations mapping wire names |
| Nullability | Fields may be absent or null | Primitives cannot be null; use wrappers |
| Numbers | One number type | Choose int/long/BigDecimal by hand |
| Money precision | double under the hood | BigDecimal is exact; double is not |
| Arrays | Native | List<T> or T[] generics |
| Nesting | Nested objects | Nested POJOs or Map |
| Immutability | Not applicable | record (Java 16+) or final fields |
| Serialisation cost | Low | Reflection is heavier; compile-time options are faster |
Edge Cases
- Pick long over int for epoch milliseconds — int overflows into negatives immediately.
- If a JSON number arrives in scientific notation, some BigDecimal parsers produce different values; configure the precision strategy.
- An unknown enum value throws by default; production usually needs "ignore unknown" or an UNKNOWN mapping.
- Cyclic references overflow the stack during serialisation — ignore them or break the cycle with a DTO.
Common Pitfalls
- A primitive int receiving null throws an NPE or fails parsing outright.
- Snake_case keys without annotations leave every field null, without an error.
- Money as double accumulates cent-level drift and reconciliation never balances.
- Epoch milliseconds in an int overflow into negative numbers.
- A JSON null hitting a Java primitive throws an NPE; only wrapper types such as Integer or Long accept it, so choose types by nullability when modelling.
Related Tools
- JSON to JavaGenerate Java classes from sample JSON with nested types, List generics, getters/setters and JsonProperty annotations for REST API clients and DTO classes.
- Data Format ConverterConvert between JSON, YAML, TOML, XML and CSV in one place with a single paste and instant preview for seamless multi-format config and data conversion.
- JSON to YAMLConvert JSON data to YAML format with custom indentation and nested object support. Turn verbose JSON configs into clean readable YAML in a couple of seconds.
- Excel to JSONConvert Excel files (xlsx, xls, csv) to JSON format. Supports nested headers, sheet selection and key mapping. Choose the sheet and header row and keep numbers.