npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

js-sql-from-json

v1.0.12

Published

Converts a strict json object to a usable SQL select query object.

Downloads

34

Readme

Table of Contents

  1. Why to use sql-from-json
  2. Getting Started
  3. Finally Run it
  4. Support Development

Why to use sql-from-json

You should use this library when working with javascript and you are wanting to convert some json into usable sql select queries.

Getting Started

Prerequisites

This library is pretty strict on format. This is for validation reasons. A strict incoming object means a more successful sql query returned.

Install

npm install js-sql-from-json

Setup

  • You will need to determine your DB Schema.
  • The schema needs to be json formatted like so:
let tablesAndColumns = [
  {
    table: "USERS",
    columns: [
      { name: "partyNo", type: "string", length: "20" },
      { name: "name", type: "string", length: "20" },
      ...
    ],
  },
  {
    table: "Businesses",
    columns: [
      { name: "partyNo", type: "string", length: "20" },
      { name: "name", type: "string", length: "20" },
      ...
    ],
  },
  ...
]
  • Once you have your schema created. Go ahead and set the lookup table like so:
const {
  setLookupTable,
} = require("js-sql-from-json");

setLookupTable(tablesAndColumns);
  • When your lookup table is set, its time to query your schema.

Accepted Operators

  • Currently, we only allow these operators:
    • (Not) Less than - <, !<
    • (Not) Greater than - >, !>
    • (Not) Equals - =, !=
    • Less than or equal - <=
    • Greater than or equal - >=
    • (N)IN - in, nin
    • Starts With - sw
    • Ends With - ew
    • Contains - contains
    • (Not) isNull - isNull (value should be true or false for !isNull),
  • These operators will be placed in your json object.

JSON Format

let json =
  {
    type: "USERS",
    fields: [
      {
        $and: [
          {
            field: "name",
            matches: [{ value: "ben", compare: "=" }],
          }
        ],
        $or: [],
      },
    ],
    selectFields: [],
    **paramCount: 1**
  };
  • Type: This is your table name in your lookup table declared above.

  • Fields: These are the fields you are wanting to query. Must be an array.

  • $and: All these fields will be ANDed together. Must be an array.

  • $and[index].field: The name of the column of the table.

  • $and[index].matches: An array of values with the desired operator (see above for accepted operators).

  • $or: same as $and

  • selectFields: Query will return Select * From ... if no fields provided. Put fields in this array if you want it to return specific fields. Select Name From Users.

  • Param Count is a tricky one. Some SQL libraries allow params. Well our builder add the params in, but if the user is calling it back to back and concatenating the queries together, the params will get confused. So put current param count here to allow it to increment properly.

  • You also must pass in a flag to determine your SQL type. We accept these flags:

    • MySQL
    • MariaDB
    • PostgreSQL
    • SQLite
    • Oracle
    • SQLServer

Finally, run it

When you have your schema in place and your json object ready, it is time to call the library.

const {
  getSQLForJSON,
} = require("js-sql-from-json");

let json =
  {
    type: "USERS",
    fields: [
      {
        $and: [
          {
            field: "name",
            matches: [{ value: "ben", compare: "=" }],
          }
        ],
        $or: [],
      },
    ],
    selectFields: [],
  };

let query  = await getSQLForJSON(json, "SQLServer");
console.log(query.sql);
console.log(query.params);

SELECT * FROM USERS WHERE NAME = @param1
{
    param1: 'ben'
}

Support Development

If you liked using this library and want it to remain free and open in the long run, it would be great if you could help support the development of this library and other libraries in the future.

https://www.buymeacoffee.com/jssqlfromjson

Thanks