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

javascript
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 TypeMongoose TypeNotes
stringStringSchema.Types.String
integer / floatNumberSchema.Types.Number
booleanBooleanSchema.Types.Boolean
nullSchema.Types.MixedMost permissive type
array of primitives[Type]Array shorthand
array of objects[SubSchema]Nested schema reference
objectSubSchemaSeparate Schema definition

Adding Validation and Indexes

javascript
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 });

Frequently Asked Questions

No — the generator produces a minimal schema with type definitions only. Add required, unique, min, max, and other validators manually after generation.

Add { timestamps: true } as the second argument to new Schema(). This automatically adds createdAt and updatedAt fields.

Yes — add Mongoose TypeScript types: import { Document, Model, Schema, model } from 'mongoose' and define an interface extending Document for your model.

Yes — the generated code uses CommonJS require syntax. For ESM projects, change require to import and use export default model(...).

Related Tools