FormatterHQ

JSON to Mongoose Schema

Paste a sample MongoDB document and get a Mongoose schema and model, with Date, ObjectId, arrays and nested objects detected for you.

Input JSON
Output JAVASCRIPT
Ready.

How to use the JSON to Mongoose Schema

  1. Paste a sample JSON document, for example one exported from MongoDB Compass.
  2. Enter the Model name you want, such as User or Order.
  3. Tick Mark fields required, timestamps: true or ES module syntax as needed.
  4. Click Generate schema and copy the result into your models folder.

JSON to Mongoose schema in one click

This JSON to Mongoose schema generator infers a schema from one real document instead of making you type every field by hand. Paste a record from your API, a fixture or a mongosh query result, and you get a new mongoose.Schema({...}) definition plus the mongoose.model() export, ready to drop into a Node.js project.

What is a Mongoose schema?

MongoDB itself does not enforce a structure on documents. Mongoose adds one in your application: a schema declares each field's type, which fields are required, defaults and references. Mongoose uses it to cast values (for example the string "31" to the number 31), validate writes and populate related documents.

Example

Input document:

{"_id": "65f1c0a2b3d4e5f6a7b8c9d0","name": "Ann Lee","email": "ann@example.com","tags": ["admin", "beta"],"address": { "city": "Paris", "zip": "75001" },"orders": [{ "sku": "A1", "qty": 2 }],"managerId": "65f1c0a2b3d4e5f6a7b8c9d1","createdAt": "2024-03-01T10:00:00Z","updatedAt": "2024-03-02T10:00:00Z"}

Generated schema with the default options:

const mongoose = require('mongoose');const userSchema = new mongoose.Schema({name: String,email: String,tags: [String],address: {city: String,zip: String,},orders: [{sku: String,qty: Number,}],managerId: mongoose.Schema.Types.ObjectId,}, { timestamps: true });module.exports = mongoose.model('User', userSchema);

How types are detected

JSON valueMongoose type
24-character hex stringmongoose.Schema.Types.ObjectId
ISO date string, like 2024-03-01 or 2024-03-01T10:00:00ZDate
Other stringString
NumberNumber
true / falseBoolean
Nested objectNested path object
Array[Type]; arrays of objects merge all keys into one subdocument shape
null or empty arrayMixed / [Mixed]

The _id and __v fields are removed, because Mongoose adds them itself. When the document has both createdAt and updatedAt, those fields are removed too and { timestamps: true } is added, which tells Mongoose to maintain them automatically.

Options explained

  • Model name: sets the model and schema variable names. Mongoose derives the collection name by lowercasing and pluralizing it, so User is stored in users.
  • Mark fields required: writes each scalar field as { type: String, required: true }. It also adds lowercase: true, trim: true to email fields and a ref to ObjectId fields ending in Id, so managerId gets ref: 'Manager' for use with populate().
  • timestamps: true: adds the timestamps option even when the sample has no date fields.
  • ES module syntax: switches from require and module.exports to import and export default.

Tips and limitations

The schema can only be as good as the sample. A field that is null in your document becomes Mixed, and a field that is missing will not appear at all, so pick a complete, representative record. If you paste an array of documents, their keys are merged so optional fields from any record are included.

Indexes, unique constraints, enums, min/max validators and defaults are not inferred. Add them after generating. Also check ref names: managerId may actually point to a User model rather than Manager.

Privacy

The schema is generated in your browser, so customer records you paste never leave your device. Related tools: generate TypeScript types for the same document with JSON to TypeScript, translate SQL queries with SQL to MongoDB, or build a validation schema with JSON to Zod.

Frequently asked questions

How does the generator know a string is an ObjectId?
Any string of exactly 24 hexadecimal characters is typed as mongoose.Schema.Types.ObjectId. That matches how ObjectIds look in exported JSON. If a field only looks like an id by coincidence, change it back to String.
Can I paste Extended JSON with $oid and $date?
Extended JSON wrappers like {"$oid": "..."} are treated as nested objects. Export or paste relaxed JSON instead, or replace the wrappers with plain strings, so the ObjectId and Date detection works.
Does it generate TypeScript types for Mongoose?
No, the output is a JavaScript schema. With Mongoose 6 and later you can let TypeScript infer the document type from the schema, or generate an interface separately with the JSON to TypeScript tool.
Why are createdAt and updatedAt missing from my schema?
When both fields are present, the generator uses { timestamps: true } instead. Mongoose then creates and updates those two fields for you, which is more reliable than setting them by hand.
How are arrays of objects handled?
The keys of every object in the array are merged into one subdocument definition, like [{ sku: String, qty: Number }]. Each element then becomes a subdocument with its own _id unless you disable that in Mongoose.

Last updated October 3, 2026

Guides

Related tools

JSON Schema to TypeScript Turn JSON Schema into TypeScript interfaces and types with optional properties, enums, $ref definitions and JSDoc comments. JSON Schema to Sample JSON Generate realistic fake JSON that conforms to a JSON Schema — formats, enums, ranges and required fields respected. JSON Schema Validator Validate JSON against a JSON Schema (draft-07, 2019-09, 2020-12) with Ajv and get every error with its path and rule. JSON to Objective-C Generate Objective-C classes with properties from a JSON sample instantly, with nested types detected automatically. JSON to PHP Generate PHP classes with typed properties from a JSON sample instantly, with nested types detected automatically. JSON to Pike Generate Pike classes from a JSON sample instantly, with nested types detected automatically. JSON to Python Generate Python dataclasses with type hints from a JSON sample instantly, with nested types detected automatically. JSON to Ruby Generate Ruby Dry::Struct classes from a JSON sample instantly, with nested types detected automatically. JSON to Rust Generate Rust structs with Serde derives from a JSON sample instantly, with nested types detected automatically. JSON to Scala Generate Scala case classes from a JSON sample instantly, with nested types detected automatically. JSON to Swift Generate Swift Codable structs from a JSON sample instantly, with nested types detected automatically. JSON to TypeScript Generate TypeScript interfaces from a JSON sample instantly, with nested types detected automatically.