Convert MongoDB document classes to JSON!

Paste Spring Data MongoDB document classes (@Document) and get an example JSON! Handy for API tests and mock request bodies.
Spring Data MongoDB
Result
 

Convert MongoDB document classes to JSON!

Feed it a Spring Data MongoDB @Document class together with its embedded classes and records, and it builds example JSON for what a controller returns when it sends that document. The shape follows Spring Boot's default Jackson serialization, so it describes the API response, not the BSON stored in MongoDB.

It saves time on MongoDB documents full of embedded lists, Map fields and Instant timestamps, where writing a response sample by hand is tedious, and it makes the gap between storage format and response format visible.

How to use

  1. Paste the @Document class and its embedded classes or records into the Spring Data MongoDB box.
  2. The document class that no other class references becomes the root of the output.
  3. Replace the id and date placeholders with realistic values before using the JSON in docs or tests.

Example

Input (Java · Spring Data MongoDB)
@Document(collection = "devices")
public class Device {
    @Id
    private String id;
    private String serial;
    @Field("fw")
    private String firmwareVersion;
    private Map<String, Double> lastReadings;
    private List<Alert> alerts;
    private Instant registeredAt;
}

record Alert(String level, String message, Instant raisedAt) {}
Output (JSON)
{
  "id": "",
  "serial": "",
  "firmwareVersion": "",
  "lastReadings": {
    "key": 0
  },
  "alerts": [
    {
      "level": "",
      "message": "",
      "raisedAt": "1970-01-01T00:00:00Z"
    }
  ],
  "registeredAt": "1970-01-01T00:00:00Z"
}

Alert in List<Alert> is a record, so its components level, message and raisedAt become fields. Map<String, Double> becomes {"key": 0}, and Instant is a UTC string ending in Z. @Field("fw") only renames the stored field in MongoDB, so the JSON key stays firmwareVersion.

Supported features

  • Java record components, as in record Alert(String level, ...), are read as fields.
  • List and Set become one-element arrays; Map<String, T> becomes {"key": value}.
  • Instant, ZonedDateTime and OffsetDateTime give 1970-01-01T00:00:00Z, while LocalDateTime gives a string without an offset.
  • Fields typed ObjectId get a sample hex ObjectId string.
  • Classes linked with @DBRef become nested objects when they are part of the input.
  • @JsonIgnore, @JsonProperty, @JsonIgnoreProperties and inherited fields follow the same rules as the JPA page.

Use cases

Responses with many embedded documents

Structures MongoDB packs into one document, like review lists, option maps and history arrays, make long response examples. Paste the classes and every nested level is written out for you.

Comparing stored and returned shapes

Short names via @Field or fields mapped to _id make database keys differ from API keys. The output shows the API side, so you can put the two next to each other.

Notes and limitations

  • Mapping annotations such as @Field("..."), @Indexed and @TextIndexed do not change JSON keys and are not reflected.
  • @Id String id is stored as _id but leaves Jackson as id, which is what the output shows.
  • Spring Boot's default Jackson writes org.bson.types.ObjectId as an object with timestamp and date, not as a hex string. To send the string form shown here, register a ToStringSerializer or use a String id.
  • Classes missing from the input, including Spring Data types like GeoJsonPoint, become null.

FAQ

Should my @Id be ObjectId or String?

With String, Spring Data converts it to an ObjectId on save and the API returns a clean hex string. Keeping the ObjectId type means you need extra Jackson configuration for responses.

Do @DBRef fields really come back as nested objects?

@DBRef resolves the referenced document when reading (on first access if lazy), so responses contain a nested object. Manual references that store only an ObjectId or String id return just the id.

How does a free-form Map<String, Object> field look?

Its value type is unknown, so it becomes {"key": null}. Replace it with the kind of value you actually store before putting it in docs.