简体   繁体   中英

Convert Json Object To SQL Table

I want to generate sql query dynamically. I found this tool

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

And i have the following JSON object:

{
  "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. 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:

    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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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