Convert JSON to Mongoose Schema (JavaScript)!

Input JSON to generate Mongoose schemas (new Schema, mongoose.model)! For Node.js, Express and MongoDB projects.
JSON
Result
 

Convert JSON to Mongoose Schema (JavaScript)!

Paste a document you plan to store in MongoDB and get a CommonJS Mongoose schema file: fields defined with new Schema({...}), the model registered with mongoose.model(), and module.exports at the end. Save it into an Express project's models/ folder and require it as is.

Field paths keep the JSON keys exactly as they are, so the schema reads documents that already exist in the collection without renaming anything. When you paste an array of documents, keys from all of them are merged into one schema.

How to use

  1. Paste one document (an object) or several (an array). Output from mongoexport, wrapped in an array, works too.
  2. Enter the Model name (default MyModel). Spaces are fine: blog comment becomes BlogComment.
  3. Save the result as a file such as models/Comment.js and load it in a router with const Comment = require('./models/Comment').

Example

Input (JSON)
[
  { "author": "mina", "body": "Great post!", "likes": 3, "approved": true, "posted_at": "2024-05-01T09:30:00Z" },
  { "author": "leo", "body": "Typo in step 2", "likes": 0, "approved": false, "posted_at": "2024-05-02T11:00:00Z", "reply_to": "6634a1" }
]
Output (Mongoose (JavaScript))
const mongoose = require('mongoose');
const { Schema } = mongoose;

const commentSchema = new Schema({
  _id: { type: Schema.Types.ObjectId, auto: true },
  author: { type: Schema.Types.String, required: true },
  body: { type: Schema.Types.String, required: true },
  likes: { type: Schema.Types.Number, required: true },
  approved: { type: Schema.Types.Boolean, required: true },
  posted_at: { type: Schema.Types.Date, required: true },
  reply_to: { type: Schema.Types.String }
});

const Comment = mongoose.model('Comment', commentSchema);

module.exports = Comment;

reply_to exists only on the second comment, so it is generated without required, and posted_at, an ISO date string, becomes Schema.Types.Date. Both keep their snake_case names because the schema paths must match the stored keys.

Supported features

  • Field types: numbers → Schema.Types.Number, true/false → Schema.Types.Boolean, ISO 8601 strings like 2024-05-01 or 2024-05-01T09:30:00Z → Schema.Types.Date, other strings → Schema.Types.String.
  • Quoted digits such as "01234" stay String, so postal codes and phone numbers keep their leading zeros.
  • Nested objects become Schema.Types.Mixed; an array of plain values of one kind becomes a typed array like [Schema.Types.String], anything else [Schema.Types.Mixed].
  • required: true is added only when every document has a non-empty value. Arrays never get it, because Mongoose fills a missing array with [].
  • An _id key copied from an export is dropped; the schema declares _id: { type: Schema.Types.ObjectId, auto: true } instead.
  • Keys that are not valid JavaScript identifiers, such as home-url, are written as quoted keys.

Use cases

The first model file of an Express + MongoDB API

If you already agreed on a request body with the frontend, turn that JSON into a schema draft and wire it straight into Comment.create(req.body) in your POST route.

Cleaning up a collection that grew without a schema

Pull a few documents from a collection that was filled with strict: false or the native driver. Which fields always exist and which appear only sometimes shows up immediately as the presence or absence of required.

Notes and limitations

  • The model is registered as mongoose.model('Comment', ...), and Mongoose lowercases and pluralizes that name to pick the collection: comments. Pass a third argument if your existing collection uses another name.
  • Mixed fields are not tracked for in-place changes; after editing a nested value call doc.markModified('profile') before save(), or replace the field with a sub-schema.
  • No defaults, indexes or unique constraints are generated. Add unique: true or index: true to fields you look up by.

FAQ

Can I get ES module syntax (import / export default)?

This page only produces CommonJS with require and module.exports. In a "type": "module" project, change the first two lines to import mongoose, { Schema } from 'mongoose' and the last line to export default Comment. For TypeScript or NestJS, use the JSON → Mongoose (TS) page instead.

How do I get automatic createdAt and updatedAt?

The generated code has no schema options. Add a second argument, new Schema({...}, { timestamps: true }), and Mongoose maintains both fields; remove fields with the same names that came from your JSON.

What happens if I save an empty string into a required field?

Mongoose's required validator for strings treats "" as missing. That is why fields that were empty strings in your sample are generated without required in the first place.