Convert JSON to Sequelize Schema (JavaScript)!

Input JSON to generate Sequelize models (sequelize.define, DataTypes)! For Node.js and Express projects.
JSON
Result
 

Convert JSON to Sequelize Schema (JavaScript)!

Converts JSON rows into a Sequelize model defined with sequelize.define(). Every column gets a DataTypes type and an allowNull flag, and the CommonJS file includes the id primary key, tableName and timestamps: false.

It fits an Express server on MySQL or PostgreSQL when you have sample rows for a table (a spreadsheet exported to JSON, an external API response) but no model definition yet.

How to use

  1. Paste one row as an object or several rows as an array. If some columns are empty in some rows, paste several rows so allowNull comes out right.
  2. Enter the Model name (default MyModel); it is used both in define and as the exported variable.
  3. The output assumes require('../db') returns your Sequelize instance. Adjust that path to your connection module, then save the file under models/.

Example

Input (JSON)
[
  { "email": "kim@example.com", "nickname": "kim", "age": 31, "marketing_opt_in": true, "joined_at": "2023-11-02" },
  { "email": "ana@example.com", "nickname": "ana", "age": null, "marketing_opt_in": false, "joined_at": "2024-01-15" }
]
Output (Sequelize (JavaScript))
const { Sequelize, DataTypes } = require('sequelize');
const sequelize = require('../db');

const Member = sequelize.define('Member', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true
  },
  email: {
    type: DataTypes.STRING,
    allowNull: false
  },
  nickname: {
    type: DataTypes.STRING,
    allowNull: false
  },
  age: {
    type: DataTypes.INTEGER,
    allowNull: true
  },
  marketingOptIn: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    field: 'marketing_opt_in'
  },
  joinedAt: {
    type: DataTypes.DATEONLY,
    allowNull: false,
    field: 'joined_at'
  }
}, {
  tableName: 'MEMBER',
  timestamps: false
});

module.exports = Member;

The second member's age is null, so only age gets allowNull: true. joined_at holds dates without a time, so it becomes DataTypes.DATEONLY, and the attribute joinedAt is tied back to the real column with field: 'joined_at'.

Supported features

  • Column types: integers → INTEGER (BIGINT past 2,147,483,647), decimals → DOUBLE, booleans → BOOLEAN, YYYY-MM-DD → DATEONLY, date-times → DATE, objects and arrays → JSON, other strings → STRING.
  • Only real JSON numbers become numeric columns; a quoted "01234" stays STRING.
  • Attributes are camelCase, and whenever that differs from the key, field: 'marketing_opt_in' maps the attribute to the original column name.
  • An id key in your JSON becomes the primary key instead of a second column: integers get autoIncrement, strings such as UUIDs become a plain STRING key. Without one, an auto-increment INTEGER id is added.
  • null or missing values in any row set allowNull: true; they do not influence the column type.
  • A model named UserProfile gets tableName: 'USER_PROFILE' and timestamps: false, so no createdAt/updatedAt columns are expected.

Use cases

Attaching a model to an existing table

Export the result of SELECT ... LIMIT 5 from your DB client as JSON and paste it to get define code with the column order, nullability and snake_case column names already reflected. Handy for mapping a single table without installing sequelize-auto.

Creating test tables with `sequelize.sync()`

During prototyping, build a model from sample JSON, run sync() to create the table in local SQLite or MySQL, then load seed data with bulkCreate.

Notes and limitations

  • DataTypes.STRING defaults to VARCHAR(255); switch long text to DataTypes.TEXT.
  • DATEONLY values come back as '2023-11-02' strings, not Date objects.
  • DataTypes.JSON works on MySQL, MariaDB, PostgreSQL and SQLite but not MSSQL; on PostgreSQL DataTypes.JSONB is usually the better choice.
  • tableName is upper case (MEMBER). On case-sensitive setups such as PostgreSQL or MySQL on Linux it must match the real table name exactly.
  • For money, replace DOUBLE with DataTypes.DECIMAL(12, 2) to avoid floating-point rounding.

FAQ

Can I get the class-based Model.init() style?

This page only generates sequelize.define(). The attributes object is identical in both styles, so declare class Member extends Model {} and pass the same object to Member.init(attributes, { sequelize, tableName: 'MEMBER' }). For TypeScript, use the JSON → Sequelize (TS) page.

The column is allowNull: false, but empty strings still get in.

allowNull only blocks SQL NULL. To reject empty strings too, add validate: { notEmpty: true } to the attribute.

Why field on each attribute instead of underscored: true?

underscored only converts camelCase to snake_case, so it cannot map keys like home-url or columns that are already camelCase. An explicit field keeps the exact JSON key as the column name whatever its style.