JSON to Mongoose schema
Paste a MongoDB document or an API response to get a Mongoose schema and model for a Node.js project.
Loading the tool…
Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.
How to use it
- Enter json input and root type name.
- The result appears straight away and updates as you edit.
- Copy the result, or copy a link that keeps the same values to share it.
How it is calculated
String, Number, Boolean, [T] and Mixed, with a sub-schema per nested object declared before use. A field always present and never null gets required: true; arrays do not, because Mongoose defaults them to []. Every field uses { type: X } so a key named type is not misread.
Example
Inputs: Root type name = User. Result: Generated code: import mongoose from "mongoose"; const orderSchema = new mongoose.Schema({ order_id: { type: String, required: true }, total: { type: Number, required: true }, coupon: { type: String }, }); const addressSchema = new mongoose.Schema({ city: { type: String, required: true }, postal_code: { type: String, required: true }, }); const userSchema = new mongoose.Schema({ id: { type: Number, required: true }, name: { type: String, required: true }, email: { type: String, required: true }, is_active: { type: Boolean, required: true }, balance: { type: Number, required: true }, tags: { type: [String] }, address: { type: addressSchema, required: true }, orders: { type: [orderSchema] }, }); export const User = mongoose.model("User", userSchema);; Types generated: 3; Fields: 13.
{
"id": 1024,
"name": "Sara Ahmed",
"email": "sara@example.com",
"is_active": true,
"balance": 1520.75,
"tags": ["vip", "riyadh"],
"address": { "city": "Riyadh", "postal_code": "12211" },
"orders": [
{ "order_id": "A-1", "total": 99.5, "coupon": "EID10" },
{ "order_id": "A-2", "total": 120, "coupon": null }
]
}import mongoose from "mongoose";
const orderSchema = new mongoose.Schema({
order_id: { type: String, required: true },
total: { type: Number, required: true },
coupon: { type: String },
});
const addressSchema = new mongoose.Schema({
city: { type: String, required: true },
postal_code: { type: String, required: true },
});
const userSchema = new mongoose.Schema({
id: { type: Number, required: true },
name: { type: String, required: true },
email: { type: String, required: true },
is_active: { type: Boolean, required: true },
balance: { type: Number, required: true },
tags: { type: [String] },
address: { type: addressSchema, required: true },
orders: { type: [orderSchema] },
});
export const User = mongoose.model("User", userSchema);Limits and caveats
- Number covers whole and decimal numbers; add min or validate where needed.
- Mongoose adds _id automatically to each document and each sub-schema.
Questions about this tool
Why do arrays not get required?
Mongoose creates arrays as [] by default, so required does not help detect a missing array.
Does it work with require instead of import?
Yes. Replace the first line with const mongoose = require("mongoose") and the export with module.exports.
Is my JSON sent to a server?
No. The conversion runs in your browser and the JSON never leaves your device.
