JSON to Mongoose Schema Generator

JSON Input
1
Model Name
Mongoose Schema Output

Last updated:

Jsonic's JSON to Mongoose Schema generator converts a JSON object into a Mongoose schema definition for Node.js and MongoDB. It maps JSON strings to { type: String }, numbers to { type: Number }, and booleans to { type: Boolean }. Fields present in the JSON are marked required: true; null values become { type: ..., default: null }. JSON arrays become array field definitions. Nested objects generate separate named Schema instances declared before the root schema. The output includes a require('mongoose') import and a mongoose.model() export. All processing runs in your browser with no data upload.

How to convert JSON to a Mongoose schema

  1. Paste your JSON object into the left panel, or click Example to load a sample.
  2. Optionally change the model name (default: Root).
  3. Click Generate.
  4. Copy the Mongoose schema into your Node.js project.
  5. Run npm install mongoose if not already installed.

FAQ

What is a Mongoose schema?

A Mongoose schema defines the shape of documents in a MongoDB collection. It specifies field names, types (String, Number, Boolean, Date, ObjectId, etc.), validation rules (required, min, max, enum), and defaults. Mongoose uses the schema to validate documents before saving and to provide a query interface.

How are JSON types mapped to Mongoose types?

JSON strings → { type: String }, JSON numbers (integers and decimals) → { type: Number }, JSON booleans → { type: Boolean }, JSON null → { type: ..., default: null }, JSON arrays → array field or subdocument array, JSON objects → nested Schema instance. Mongoose does not distinguish integers from floats — both use Number.

Why does the generator add required: true?

Fields present in the example JSON are marked required: true because they appear in the data. This prevents saving documents with missing required fields and throws a validation error at the application level before the document reaches MongoDB. Remove required: true manually for fields that are truly optional.

How are nested objects handled?

Each nested JSON object becomes a separate Schema instance. Nested schemas are declared before the root schema and referenced by variable name in the parent schema field definition. This is equivalent to Mongoose's subdocument pattern, which embeds the nested document directly in the parent document.

What is the difference between a nested Schema and a ref/populate?

A nested Schema (subdocument) embeds the data directly in the parent document — one MongoDB document contains all the data. A ref/populate stores a reference (ObjectId) to a separate collection and fetches it with a second query. Use subdocuments for data that is always fetched together; use refs for data shared across multiple documents.

How do I add validation to the generated schema?

Mongoose schema types support built-in validators: minlength/maxlength for strings, min/max for numbers, enum for fixed value sets, match for regex patterns. For example: { type: String, required: true, minlength: 1, maxlength: 255 }. Add these manually after generating.

Does the generated schema include timestamps?

No, timestamps are not added by default. Add { timestamps: true } as a second argument to the Schema constructor: new Schema({...}, { timestamps: true }). This automatically adds createdAt and updatedAt fields that Mongoose manages.

Can I use the generated schema with TypeScript?

Yes. For TypeScript, add an interface and use Schema<InterfaceName>. Replace the require() syntax with import mongoose from "mongoose"; import { Schema, model, Document } from "mongoose";. Then type the schema: const RootSchema = new Schema<IRoot>({...}) where IRoot extends the Document interface.