如何解析JSON属性而不显式引用它们?

时间:2018-07-02 13:32:58

标签: sql json sql-server

我正在将某些SQL Server与具有某些属性的JSON对象一起使用。 JSON学生对象看起来像这样:

SET @studentObject= 
    '{
    "Student": {
    "Id":"' + CAST(NEWID() as NVARCHAR(36)) +'",
    "Name": "StudentA",
    "FavoriteSubject": "Math",
    "Grade": 8
    }
}'

我想提取这些属性(Id,Name,FavoriteSubject和Grade),而无需显式引用它们。本质上是@studentObject类型查询的SELECT ALL PROPERTIES。

现在,我有类似的内容,但我正在尝试使此查询更通用:

 SELECT S.Name, S.Address, 
 JSON_VALUE(Value, '$.Student.Id') AS Id,
 JSON_VALUE(Value, '$.Student.Name') AS StudentName,
 JSON_VALUE(Value, '$.Student.FavoriteSubject') AS FavoriteSubject,
 JSON_VALUE(Value, '$.Student.Grade') AS Grade

 FROM School S;

我试图做一个

SELCT S.Name, S.Address, @studentObjects but it was throwing an exception about an incorrect number of values.

0 个答案:

没有答案