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 🙏

© 2025 – Pkg Stats / Ryan Hefner

json-fast-sql

v1.0.2

Published

快速实现JSON转SQL语句

Readme

json-fast-sql

一款快速实现JSON转SQL语句的工具包,主要针对大量JSON数据存储数据库问题。

by 文艺倾年

项目作用

  1. 预处理JSON,纠正JSON错误的符号格式
  2. 扁平化JSON,提取JSON属性并校验合法性
  3. 根据json生成创建表语句,插入语句
  4. 根据表数据导出各字段记录的01分布情况

应用场景

如果你的大量JSON数据存在很多符号问题(如不恰当的引号问题),嵌套子句繁多(如对象嵌套对象),JSON属性过多手动建表费时,那么推荐使用该工具进行简化你的工作。

之所以会有这个轮子,也正是因为最近小航遇到了类似这样的问题,头疼的JSON,甚至JSON夹杂很多脏JSON!

示例

需求:

json大概是这样的,存在引号问题,有数组,有对象嵌套。接下来演示如何使用本工具简化工作:

{
	"id": "1",
	"name": "artBoy",
	"info": "{
		"hobby": ["reading", "piano", "studying"],
		"age": 20,
		"sex": "男",
		"sign": "凡是不能"摧毁"我的,都必将使我强大!"
	}"
}

这里我们使用压缩版(因为要进行预处理):
{"id":"1","name":"artBoy","info":"{"hobby":["reading","piano","studying"],"age":20,"sex":"男","sign":"凡是不能"摧毁"我的,都必将使我强大!"}"}

1.安装:

yarn安装:

yarn add json-fast-sql

npm安装:

npm install json-fast-sql

2.使用:

const jfs = require('json-fast-sql')
// 1.读取test.json,开启预处理,扁平化,提取属性数组
let res1 = jfs.readJson('test.json', true)
let res2 = jfs.flattenArray(res1);
let{attrArray, dataArray} = jfs.extractAttr(res2)
// // 2.生成创建表语句
let res3 = jfs.createTable(attrArray, 'mytable');
console.log(res3);
// // 3.生成插入语句
let res4 = jfs.insertTableBatch(dataArray, 'mytable')
console.log(res4);

输出语句:

CREATE TABLE mytable ( id text(500), name text(500), info_hobby_0 text(500), info_hobby_1 text(500), info_hobby_2 text(500), info_age text(500), info_sex text(500), info_sign text(500) )
[
  'INSERT INTO mytable(`id`,`name`,`info_hobby_0`,`info_hobby_1`,`info_hobby_2`,`info_age`,`info_sex`,`info_sign`) VALUES("1","artBoy","reading","piano","studying","20","男","凡是不能'摧毁'我的,都必将使我强大!")'
]

文档

引入:

const jfs = require('json-fast-sql')

方法:

readJson:读取JSON

/**
 * @param path json文件路径,支持大量json一次传入,文件格式不限
 * @param isProJson 是否开启预处理JSON
 * @return json 处理好的json
 */
const readJson = (path, isProJson = true) => {
    return json;
}

flatten、flattenArray:扁平化JSON

/**
 * @param data json
 * @return json 处理好的json
 */
const flatten = (data) => {
    return result;
}

/**
 * @param data json数组
 * @return json 处理好的json数组
 */
const flattenArray = (data) => {
    return result;
}

extractAttr:提取JSON

/**
 * @param dataArray json数组
 * @return json 属性数组和纠正后的json数组
 */
const extractAttr = (dataArray) => {
    return {attrArray, dataArray};
}

createMatrix:生成属性分布矩阵

/**
 * @param attrArr 属性数组
 * @param dataArray json数组
 * @return booleanMatrixList 布尔矩阵
 */
const createMatrix = (attrArr, dataArray) => {
    return booleanMatrixList;
}

createTable:生成创建表语句

/**
 * @param attrArr 属性数组
 * @param tableName 表名
 * @return sql sql语句
 */
const createTable = (attrArr, tableName = "mytable") => {
    return sql;
}

insertTableBatch:生成创建表语句

/**
 * @param dataArray json数组
 * @param tableName 表名
 * @return sql sql语句
 */
const insertTableBatch = (dataArray, tableName = "mytable") => {
    return sql;
}