javascript - 将 Json 对象转换为 SQL 表

标签 javascript jquery sql json query-builder

我想动态生成sql查询。我找到了这个工具

  http://querybuilder.js.org/demo.html

我有以下 JSON 对象:

{
  "condition": "AND",
  "rules": [
    {
      "id": "name",
      "field": "name",
      "type": "string",
      "input": "text",
      "operator": "equal",
      "value": "zura"
    },
    {
      "condition": "OR",
      "rules": [
        {
          "id": "category",
          "field": "category",
          "type": "integer",
          "input": "select",
          "operator": "equal",
          "value": "1"
        },
        {
          "id": "price",
          "field": "price",
          "type": "double",
          "input": "number",
          "operator": "equal",
          "value": "123"
        }
      ]
    },
    {
      "id": "in_stock",
      "field": "in_stock",
      "type": "integer",
      "input": "radio",
      "operator": "equal",
      "value": "1"
    },
    {
      "condition": "AND",
      "rules": [
        {
          "id": "category",
          "field": "category",
          "type": "integer",
          "input": "select",
          "operator": "equal",
          "value": "2"
        },
        {
          "id": "in_stock",
          "field": "in_stock",
          "type": "integer",
          "input": "radio",
          "operator": "equal",
          "value": "0"
        }
      ]
    }
  ]
}

现在我想生成 SQL 表以便正确保存此 JSON 数据。 有没有办法生成表格,如果是,请给我链接或请帮助我创建相同的表格

最佳答案

您的 Json 数据需要使用递归 SQL‌ 函数进行解码。您首先需要创建一个自引用表,如下所示:

    CREATE TABLE jsonCondition(
ConditionId INT IDENTITY,
ParentCondotionId INT ,
Id NVARCHAR(20),
Field NVARCHAR(20),
Type NVARCHAR(20),
Input NVARCHAR(20),
Operator NVARCHAR(20),
Value NVARCHAR(20) 
)

那么请引用我的另一个 json recursive Conversion to SQL: How to generate hierarchical JSON data with Microsoft SQL Server 2016?

关于javascript - 将 Json 对象转换为 SQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43769297/

相关文章:

asp.net - 在 JavaScript 中嵌入 ASP.NET 服务器代码

javascript - 如何使用 jQuery 将 ajax 调用的结果存储在函数中?

jquery - 动态创建的 iFrame 上的 YouTube iFrame API

javascript - 在 li 中显示和隐藏 div

SQLite3 : exit indent

javascript - 在局部变量中保存重复访问的对象属性是否有缺点?

javascript - $(selector, element) 原生 JS 替代方案

javascript - 搜索自定义日期选择器

php - 从 SQL 表中检索数据

sql - 从远程(链接)SQL 服务器选择记录时,瓶颈在哪里/有哪些陷阱?