Skip to content

Tertiumnon/json-to-sql-script

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON to SQL script

This package creates SQL script from JSON-file.

Install

npm i -g json-to-sql-script

Arguments

'-s': '--source' // Source file path (required)
'-o': '--output' // Output file path (required)
'-n': '--name' // Main table name (required)
'-e': '--excludes' // Exclude property names

Example

json-to-sql-script -s 'input.json' -o 'output.sql' -n 'items' -e 'date_created|date_modified'

Example output

{
  "data": [
    {
      "name": "Rye bread",
      "description": "Rye bread is a type of bread made with various proportions of flour from rye grain."
    },
    {
      // next object
    }
  ]
}
INSERT IGNORE INTO items (name,description) VALUES ("Rye bread","Rye bread is a type of bread made with various proportions of flour from rye grain.");

If JSON object includes arrays or objects, for example:

{
  "data": [
    {
      "name": "Rye bread",
      "description": "Rye bread is a type of bread made with various proportions of flour from rye grain.",
      "components": [
        {
          "name": "Salt"
        },
        {
          "name": "Sugar"
        }
      ]
    },
    {
      // next object
    }
  ]
}

Script adds rows with ref_{{table_name}}_name:

INSERT IGNORE INTO items_components (name,ref_item_name) VALUES ("Salt","Rye bread.");

About

Convert your JSON-file to SQL-script for inserting to database.

Resources

Stars

Watchers

Forks

Packages

No packages published