01What is JSON to Mongoose Schema?
Writing a Mongoose model by hand from a big API response is slow and easy to get wrong. Paste one sample document here and you get a ready model file back, with every field typed from its value. Strings become String, numbers Number and booleans Boolean. A null value becomes Schema.Types.Mixed with required: false, since one null tells us nothing about the real type. Two string patterns get special treatment. A string of exactly 24 hex characters is typed as Schema.Types.ObjectId, which is how MongoDB writes a 12-byte ObjectId as text. A string shaped like an ISO 8601 date, such as 2024-01-15 or 2024-01-15T10:30:00Z, becomes Date. Nested objects turn into inline sub-documents. An array of objects becomes an array of sub-documents built from the first element, and an array of plain values takes the type of its first item. An empty array falls back to Mixed. If you paste a whole array of documents, only the first one is used. Every field that is not null gets required: true, and the schema always carries timestamps: true, so Mongoose adds createdAt and updatedAt for you. The Model name box sets both the schema variable (userSchema) and the model name (User). The format switch picks CommonJS with require and module.exports, or an ES module with import and export default. Here is a quick example. Paste {"location":{"type":"Point","coordinates":[-73.97,40.77]}} with the model name Place. You get a placeSchema where location holds a nested type field of String plus coordinates as an array of Number. That nested type key is the GeoJSON pattern the Mongoose docs describe for points. Odd keys are handled too. A key like first-name or 2fa is quoted, a __proto__ key is dropped, and a model name starting with a digit gets a Model_ prefix. Now the honest part. The tool reads a single sample, not your collection, so a field that is a string here and a number elsewhere will be typed wrong. Dates must be real calendar dates, so 2024-13-45 stays a String. The ObjectId check is a pattern match only, so any 24-character hex value, such as a short hash, is typed as ObjectId. ObjectId fields come out without a ref, and no indexes, enums or validators are added. Your JSON is parsed and converted in the page itself. It never reaches a server or a database.
Node.js developers on Express or Next.js backends who need a first Mongoose model from a real JSON payload instead of typing every field. The tool runs entirely in your browser, so your data never leaves your device.
02How to use JSON to Mongoose Schema
- Paste a JSON object, or an array of objects, into JSON Input.
- Type a Model name such as User or Order. The schema variable and model name are built from it.
- Pick CommonJS or ES Module to match how your project loads files.
- Read the Mongoose Schema output. If a JSON Parse Error box appears, fix the input it points to.
- Click Copy and save the result as a model file, then add refs, indexes and enums by hand.
03Worked examples
- Input:
{"email":"[email protected]","createdAt":"2026-01-05T10:00:00Z","owner":"507f1f77bcf86cd799439011","tags":["x"],"meta":null} (CommonJS, model User) - Result:
const mongoose = require('mongoose'); const { Schema } = mongoose; const userSchema = new Schema({ email: { type: String, required: true, }, createdAt: { type: Date, required: true, }, owner: { type: Schema.Types.ObjectId, required: true, }, tags: [{ type: String, }], meta: { type: Schema.Types.Mixed, required: false, }, }, … - ISO date strings become Date and 24-character hex strings become ObjectId; null becomes Mixed and is not required. Output is cut off before the options and export.
- Input:
{"class":1,"items":[{"id":1},{"id":2,"qty":3}]} (ES Module, model Order) - Result:
import mongoose from 'mongoose'; const { Schema } = mongoose; const orderSchema = new Schema({ class: { type: Number, required: true, }, items: [{ id: { type: Number, required: true, }, }], }, { timestamps: true, }); export default mongoose.model('Order', orderSchema); - The subdocument shape comes from the first array item only, so qty (which appears only in the second item) is missing. Put your fullest item first.
- Input:
{"location":{"type":"Point","coordinates":[-73.97,40.77]}} (ES Module, model Place) - Result:
const placeSchema = new Schema({ location: { type: { type: String, required: true, }, coordinates: [{ type: Number, }], }, }, { timestamps: true, }); - GeoJSON point shape. Import and export lines are trimmed from this output.
- Input:
{"first-name":"Ann","2fa":false,"__proto__":{"x":1}} (ES Module, model 2024 users) - Result:
const model_2024UsersSchema = new Schema({ "first-name": { type: String, required: true, }, "2fa": { type: Boolean, required: true, }, }, … export default mongoose.model('Model_2024Users', model_2024UsersSchema); - Invalid identifiers are quoted, __proto__ is dropped and the digit-led model name gets a Model_ prefix. Middle lines trimmed.
- Input:
{"due":"2024-13-45","hash":"abcdefabcdefabcdefabcdef"} (ES Module, model Task) - Result:
due: { type: String, required: true, }, hash: { type: Schema.Types.ObjectId, required: true, }, - A month of 13 is not a real date, so due stays a String. A 24-character hex hash still gives ObjectId. Fields only shown.
- Input:
"hello" - Result:
Input must be a JSON object or array of objects - Shown in the JSON Parse Error box. A bare string, number or empty array produces an error instead of a schema.
04Common use cases
- Scaffolding a model from a sample API response when you start an Express and MongoDB backend.
- Moving an existing collection under Mongoose by pasting one document exported from MongoDB Compass.
- Modelling a third-party webhook payload, such as an order event, that you need to store.
- Generating ES module models for a Next.js API route that talks to MongoDB.
- Storing GeoJSON points for a store locator and getting the nested type and coordinates shape right.
- Showing students in a workshop how JSON values map to Mongoose schema types.
05Tips
- ObjectId fields have no ref. Add ref: 'User' (or the right model) yourself, otherwise populate() has nothing to follow.
- Only the first array element is read, so put your most complete item first in any array of objects.
- ObjectId detection is pattern based. Check any field typed as ObjectId that is really a hash or a code.
- Every field starts as required: true. Relax the optional ones before you save real data, or inserts will fail validation.
- Arrays of plain values come out without required. An empty array in your sample becomes Mixed, so give it one real item if you know the type.
06Related concepts
Mongoose, MongoDB, Schema design, ObjectId, Sub-documents, ODM.
Frequently Asked Questions
What is JSON to Mongoose Schema?
JSON to Mongoose Schema is a free, browser-based developer tool on AIDToolStack. Convert JSON to Mongoose schema definitions It requires no installation or sign-up and works entirely in your browser.
Is JSON to Mongoose Schema free to use?
Yes. No sign-up, no fees and no usage cap.
Does JSON to Mongoose Schema store my data?
No. It runs in your browser; nothing is uploaded.