读取JSON数据时,从OPENJSON()接收“ NULL”返回

时间:2019-08-02 07:26:30

标签: json sql-server

我需要将数据从一个单独的JSON文件中提取到我的SQL表中,但是,我似乎无法得出正确的结果。 它一直返回“ NULL”,我也不十分清楚为什么-我怀疑它是OPENJSON()中列出的JSON路径,但似乎无法正确执行。

JSON

{
    "isMoneyClient": false,
    "showPower": true,
    "removeGamePlay": true,
    "visualTweaks": {
      "0": {
        "value": true,
        "name": "Clock"
      },
      "1": {
        "value": true,
        "name": "CopperIcon"
      }
   }
}

SQL

DECLARE @JSON VARCHAR(MAX)
SELECT @JSON = BulkColumn
FROM OPENROWSET 
(BULK 'C:\config.json', SINGLE_CLOB) 
AS A

UPDATE dbo.CommonBaseline
SET CommonBaseline.Config_isMoneyClient =  isMoneyClient ,
    CommonBaseline.Config_showPower =  showPower , 
    CommonBaseline.Config_removeGamePlay =  removeGamePlay
     FROM OPENJSON (@JSON)
        WITH ( 
            isMoneyClient Varchar (50),
            showPower Varchar (50),
            removeGamePlay Varchar (50)
            )

要求我低于通用基准计划

CREATE TABLE CommonBaseline (
    ServerID int NOT NULL PRIMARY KEY,
    Config_isMoneyClient varchar(255) ,
    Config_showPower varchar(255) ,
    Config_removeGamePlay varchar(255)
);

2 个答案:

答案 0 :(得分:0)

请检查您的CommonBaseline表架构。列Config_isMoneyClientConfig_showPowerConfig_removeGamePlay

的数据类型

在以下情况下,它运行正常

DECLARE @JSON VARCHAR(MAX)
SELECT @JSON = BulkColumn
FROM OPENROWSET 
(BULK 'e:\config.json', SINGLE_CLOB) 
AS A

or 

SELECT @JSON = '{
"isMoneyClient": false,
"showPower": true,
"removeGamePlay": true,
"visualTweaks": {
  "0": {
    "value": true,
    "name": "Clock"
  },
  "1": {
    "value": true,
    "name": "CopperIcon"
   }
 }
}'

declare @CommonBaseline as Table 
( id int,
Config_isMoneyClient bit,
Config_showPower bit,
Config_removeGamePlay bit )

insert into @CommonBaseline ( id ) values ( 1 )  ---- please check your table CommonBaseline as atleast 1 record to update the same

UPDATE @CommonBaseline 
SET Config_isMoneyClient =  isMoneyClient ,
    Config_showPower =  showPower , 
    Config_removeGamePlay =  removeGamePlay
     FROM OPENJSON (@JSON)
        WITH ( 
            isMoneyClient Varchar (50),
            showPower Varchar (50),
            removeGamePlay Varchar (50)
            )
select * from @CommonBaseline

注意:请检查您在CommonBaseline中是否已有行。由于您使用的是update

答案 1 :(得分:0)

更新

这是尝试改善此答案的尝试。当您错过使用column path子句在OPENJSON()调用中的WITH定义时,JSON表达式中的键与列名之间的匹配为区分大小写 。这是产生意外NULL结果的另一个可能原因,因为您没有在OPENJSON()调用中使用列路径。

示例:

DECLARE @json nvarchar(max) = N'{
    "isMoneyClient": false,
    "showPower": true,
    "removeGamePlay": true,
    "visualTweaks": {
      "0": {
        "value": true,
        "name": "Clock"
      },
      "1": {
        "value": true,
        "name": "CopperIcon"
      }
   }
}'

-- NULL results. 
-- The column names (IsMoneyClient) and 
-- the keys in JSON expression (isMoneyClient) don't match.
SELECT *
FROM OPENJSON (@json) WITH ( 
   IsMoneyClient Varchar (50),
   ShowPower Varchar (50),
   RemoveGamePlay Varchar (50)
)

-- Expected results. The correct column paths are used
SELECT *
FROM OPENJSON (@json) WITH ( 
   IsMoneyClient Varchar(50) '$.isMoneyClient',
   ShowPower Varchar(50) '$.showPower',
   RemoveGamePlay Varchar(50) '$.removeGamePlay'
)

原始答案:

对您意想不到的结果的一种可能解释是,在某些情况下,即使您的JSON内容无效,OPENJSON()也只会读取其中一部分内容。我可以在下一个示例中重现它:

声明:

-- JSON
-- Valid JSON is '[{"name": "A"},{"name": "B"}]'
DECLARE @json nvarchar(max) = N'
   {"name": "A"},
   {"name": "B"}
'
-- Read JSON content
SELECT * FROM OPENJSON(@json, '$')
SELECT * FROM OPENJSON(@json, '$') WITH (
   [name] nvarchar(100) '$.name'
)

输出(OPENJSON()仅读取{"name": "A"}输入中的JSON部分):

----------------
key value   type
----------------
name    A   1

----
name
----
A

这里的一种解决方案是使用JSON检查您的ISJSON内容:

IF ISJSON(@json) = 1 PRINT 'Valid JSON' ELSE PRINT 'Not valid JSON';

如果可能,请尝试修正输入JSON

声明:

-- JSON
-- Valid JSON is '[{"name": "A"},{"name": "B"}]'
DECLARE @json nvarchar(max) = N'
   {"name": "A"},
   {"name": "B"}
'
-- Read JSON content    
SELECT * FROM OPENJSON(CONCAT('[', @json, ']'), '$') WITH (
   [name] nvarchar(100) '$.name'
)

输出:

---- 
name
---- 
A
B