简体   繁体   English

将Json对象转换为SQL表

[英]Convert Json Object To SQL Table

I want to generate sql query dynamically. 我想动态生成SQL查询。 I found this tool 我找到了这个工具

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

And i have the following JSON object: 我有以下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"
        }
      ]
    }
  ]
}

Now i want to generate SQL Table in order to save this JSON data properly. 现在我想生成SQL表,以便正确保存这个JSON数据。 Is there any way to generate Table, if yes please give me link or please help me to create same table 有没有办法生成表,如果是的请给我链接或请帮我创建相同的表

This is pretty basic but should work. 这是非常基本但应该有效。 Replace the table name with whatever works for you. 将表名替换为适合您的表名。 The field sizes are pretty wide but I do not know what your input values are going to be unless you provide additional information. 字段大小相当宽,但除非您提供其他信息,否则我不知道您的输入值是什么。

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

Your Json data needs to be decoded using recursive SQL‌ function.You first need to create a self-referenced table like this: 您的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) 
)

then please refer to my other json recursive Conversion to SQL at: How to generate hierarchical JSON data with Microsoft SQL Server 2016? 那么请参考我的其他json recursive转换为SQL: 如何使用Microsoft SQL Server 2016生成分层JSON数据?

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

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