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 value | Mongoose type |
|---|---|
| 24-character hex string | mongoose.Schema.Types.ObjectId |
ISO date string, like 2024-03-01 or 2024-03-01T10:00:00Z | Date |
| Other string | String |
| Number | Number |
true / false | Boolean |
| Nested object | Nested path object |
| Array | [Type]; arrays of objects merge all keys into one subdocument shape |
null or empty array | Mixed / [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
Useris stored inusers. - Mark fields required: writes each scalar field as
{ type: String, required: true }. It also addslowercase: true, trim: trueto email fields and arefto ObjectId fields ending inId, somanagerIdgetsref: 'Manager'for use withpopulate(). - timestamps: true: adds the timestamps option even when the sample has no date fields.
- ES module syntax: switches from
requireandmodule.exportstoimportandexport 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.