JSON to Mongoose Schema
Generate Mongoose Schema definitions with nested sub-schemas from any JSON object.
What is Mongoose?
Mongoose is an Object Document Mapper (ODM) for MongoDB and Node.js. It lets you define schemas for your MongoDB documents — enforcing structure, types, and validation on a database that is schema-less by default. A Mongoose Schema specifies the shape of the document: what fields exist, their types, and optional constraints like required, unique, or min/max.
When you have a JSON response from an API or a sample MongoDB document, writing the Mongoose Schema by hand is tedious. This tool reads your JSON and generates the complete Schema definition with all nested sub-schemas, proper type mappings, and a model export — ready to paste into your Node.js or Express application.
Generated Schema Example
const { Schema, model } = require('mongoose');
const AddressSchema = new Schema({
street: { type: String },
city: { type: String },
});
const UserSchema = new Schema({
name: { type: String },
email: { type: String },
age: { type: Number },
verified: { type: Boolean },
address: AddressSchema,
tags: [{ type: String }],
});
module.exports = model('User', UserSchema);Type Mapping
| JSON Type | Mongoose Type | Notes |
|---|---|---|
| string | String | Schema.Types.String |
| integer / float | Number | Schema.Types.Number |
| boolean | Boolean | Schema.Types.Boolean |
| null | Schema.Types.Mixed | Most permissive type |
| array of primitives | [Type] | Array shorthand |
| array of objects | [SubSchema] | Nested schema reference |
| object | SubSchema | Separate Schema definition |
Adding Validation and Indexes
const UserSchema = new Schema({
email: { type: String, required: true, unique: true, lowercase: true },
age: { type: Number, min: 0, max: 150 },
name: { type: String, trim: true, maxlength: 100 },
createdAt: { type: Date, default: Date.now },
}, {
timestamps: true, // adds createdAt and updatedAt automatically
});
UserSchema.index({ email: 1 });