Convert JSON to Spring Boot MongoDB Schema!

Input JSON to generate Spring Boot / Spring Data MongoDB document classes (@Document, @Id)!
JSON
Result
 

Convert JSON to Spring Boot MongoDB Schema!

Converts JSON documents into a Spring Data MongoDB @Document class: the collection named in @Document, an @Id field, a private field per key with @Field where the stored key differs, plus Lombok and jakarta.validation annotations, all in one Java file.

Use it when data that lands in MongoDB as plain JSON (IoT readings, events, scraped results) needs to be read and written from a Spring Boot service through a MongoRepository.

How to use

  1. Paste documents that are or will be in the collection. Pasting several as an array, such as the output of find().limit(5) in mongosh, separates occasional fields from constant ones.
  2. Type a Model name (empty means MyModel); it is used for both the class name and the collection in @Document(collection = "...").
  3. Fix the package line, save the file, and create a repository extending MongoRepository<DeviceLog, String>.

Example

Input (JSON)
[
  { "device_id": "th-sensor-07", "temperature": 21.4, "battery": 87, "online": true, "recorded_at": "2024-02-11T06:00:00Z" },
  { "device_id": "th-sensor-09", "temperature": 19.8, "battery": 12, "online": false, "recorded_at": "2024-02-11T06:00:00Z", "firmware": "1.4.2" }
]
Output (Spring Data MongoDB)
package com.hello.world.devicelog;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

import java.time.LocalDateTime;

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

@Document(collection = "DeviceLog")
@FieldNameConstants
@Getter
@Setter
public class DeviceLog {
    @Id
    private String id;

    @NotEmpty()
    @Field("device_id")
    private String deviceId;

    @NotNull()
    private Double temperature;

    @NotNull()
    private Long battery;

    @NotNull()
    private Boolean online;

    @NotNull()
    @Field("recorded_at")
    private LocalDateTime recordedAt;

    private String firmware;
}

firmware appears only in the second document, so it has no annotation. device_id and recorded_at are read through @Field("device_id") and @Field("recorded_at"), so existing documents map onto the camelCase fields without migration.

Supported features

  • Names the collection explicitly with @Document(collection = "DeviceLog").
  • @Id private String id holds MongoDB's _id; an id or _id key in your JSON is folded into it rather than repeated, and a numeric id makes it Long.
  • Each field is typed from its values as Long, Double, Boolean, LocalDate (YYYY-MM-DD), LocalDateTime (date-times) or String; embedded objects become Map<String, Object> and arrays List<...>.
  • @Field("...") is added whenever the Java name is not the stored key, including Java keywords renamed with a trailing _.
  • Fields present in every document get @NotEmpty() for non-empty strings and @NotNull() otherwise; occasional or null fields get none.

Use cases

Query APIs over sensor or log collections

When readings that devices write straight into MongoDB must be served to a dashboard, paste a few documents to get the class and immediately declare derived queries such as findByDeviceIdOrderByRecordedAtDesc.

Sharing a collection with a Node.js service

If Spring needs to read a collection another service already writes in snake_case, the @Field annotations let the Java side use camelCase names while reading and writing the same keys.

Notes and limitations

  • @NotEmpty and @NotNull are not checked on save by default; register a ValidatingMongoEventListener bean to validate right before save().
  • LocalDateTime is stored as a UTC-based Date in MongoDB and can appear shifted depending on the server time zone. For logs where the exact instant matters, Instant is safer.
  • A Long id is not generated by MongoDB; you must assign it before saving. Only String, ObjectId and BigInteger ids are filled in automatically.
  • Embedded documents come out as Map<String, Object>. Replace them with small classes when you want typed access and validation.

FAQ

Why is the field called id when MongoDB stores _id?

Spring Data maps a field named id (or annotated @Id) to _id automatically and converts a String to an ObjectId when it looks like one. Declaring _id yourself is unnecessary.

I want the collection to be called device_logs.

Just edit the string: @Document(collection = "device_logs"). The generator inserts the model name as typed, so entering device_logs as the model name would also turn the class into DeviceLogs.

How does this differ from a JPA entity?

It uses @Document, @Field and Spring Data's @Id instead of @Entity, @Column and @GeneratedValue, and nested values stay embedded instead of needing a JSON column. For relational tables, use the JSON → Spring Boot JPA page.