Convert JSON to Spring Boot JPA Schema!

Input JSON to generate Spring Boot / Spring Data JPA entities (@Entity, @Id, @GeneratedValue, Hibernate) with Lombok @Getter/@Setter!
JSON
Result
 

Convert JSON to Spring Boot JPA Schema!

Converts JSON into an @Entity class for Spring Data JPA on Spring Boot 3. The single Java file includes jakarta.persistence and jakarta.validation imports, an @Id primary key, a private field per key, Lombok's @Getter, @Setter and @FieldNameConstants, and Bean Validation's @NotEmpty/@NotNull.

It is aimed at Spring Boot backend work where you need an entity mapped to a table quickly, starting from sample JSON from an integration partner or a screen spec.

How to use

  1. Paste one record as an object or several as an array. Fields missing from any record get no validation annotation.
  2. Enter the entity class name in Model name (default MyModel). Its lowercase form is also used at the end of the package (com.hello.world.reservation).
  3. Change the package line to your project's package, save it as Reservation.java, and create a repository extending JpaRepository<Reservation, Long>.

Example

Input (JSON)
{
  "guest_name": "Hana Lee",
  "room_no": 1204,
  "nights": 3,
  "rate": 149.5,
  "breakfast": true,
  "check_in": "2024-08-09T15:00:00"
}
Output (Spring Data JPA)
package com.hello.world.reservation;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

import java.time.LocalDateTime;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.FieldNameConstants;

@Entity
@FieldNameConstants
@Getter
@Setter
public class Reservation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotEmpty()
    @Column(name = "guest_name")
    private String guestName;

    @NotNull()
    @Column(name = "room_no")
    private Long roomNo;

    @NotNull()
    private Long nights;

    @NotNull()
    private Double rate;

    @NotNull()
    private Boolean breakfast;

    @NotNull()
    @Column(name = "check_in")
    private LocalDateTime checkIn;
}

Integers like room_no and nights became Long, rate Double, and check_in LocalDateTime. Each snake_case key gets @Column(name = "..."), and since every value is filled in, the String field guestName gets @NotEmpty() and the others @NotNull().

Supported features

  • Java types: integers → Long, decimals → Double, booleans → Boolean, YYYY-MM-DD → LocalDate, date-times → LocalDateTime, other strings (digit-only ones included) → String.
  • Objects become Map<String, Object> and arrays List<...>, both annotated @JdbcTypeCode(SqlTypes.JSON) so Hibernate 6 stores them in a JSON column.
  • @Column(name = "guest_name") keeps the original key as the column name whenever the Java field name differs; keys that are Java keywords, like class, become class_.
  • The primary key follows your JSON: no id or an integer id gives @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id, a string id gives a plain @Id private String id.
  • Always-present fields get @NotEmpty() when they are non-empty strings and @NotNull() otherwise, since @NotEmpty only applies to strings and collections.
  • Lombok @FieldNameConstants generates name constants such as Reservation.Fields.guestName.

Use cases

Batch jobs that store external API data

When reservations or orders from another system must be pulled periodically and saved, one response JSON gives you the entity, and the batch job skeleton around saveAll() follows quickly.

Development tables via `ddl-auto`

With spring.jpa.hibernate.ddl-auto=update enabled locally, Hibernate creates the table from the generated entity, so you can load data for UI work right away.

Notes and limitations

  • Imports target Jakarta EE (Spring Boot 3, Hibernate 6). On Spring Boot 2, change jakarta. back to javax. and replace @JdbcTypeCode with a converter, since it does not exist in Hibernate 5.
  • 149.0 in JSON parses to the same number as 149, so it becomes Long. A decimal field needs a fractional value in the sample to come out as Double, and money is better served by BigDecimal.
  • Timestamps with an offset, like 2024-08-09T15:00:00+09:00, lose the offset in LocalDateTime. Use OffsetDateTime or Instant when the instant matters.
  • JSON columns need a database that has them (MySQL 5.7+, PostgreSQL, H2 2.x). For real relations, replace the Map with an @Embedded class or an @OneToMany association.

FAQ

Why put @Column(name = ...) on fields Spring would map to snake_case anyway?

Spring Boot's default naming strategy happens to turn guestName into guest_name, but that breaks as soon as a key is not plain snake_case or the strategy is changed. The explicit name keeps the mapping correct regardless of configuration.

Is @NotNull enforced when saving?

With spring-boot-starter-validation (Hibernate Validator) on the classpath, Hibernate runs Bean Validation right before persist and update and throws ConstraintViolationException. Without it, the annotations are ignored.

What is @FieldNameConstants for?

Lombok generates string constants such as Reservation.Fields.checkIn. Use them in Sort.by(Reservation.Fields.checkIn) or in Specification/Criteria queries instead of string literals, so renaming a field breaks the build rather than the query.