繁体   English   中英

将Json对象转换为SQL表

[英]Convert Json Object To SQL Table

我想动态生成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数据。 有没有办法生成表,如果是的请给我链接或请帮我创建相同的表

这是非常基本但应该有效。 将表名替换为适合您的表名。 字段大小相当宽,但除非您提供其他信息,否则我不知道您的输入值是什么。

CREATE TABLE [NameYourTableHere]
(Name VARCHAR(MAX),
Category BIGINT,
Price DECIMAL(19,2),
In_Stock INT)

您的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转换为SQL: 如何使用Microsoft SQL Server 2016生成分层JSON数据?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM