简体   繁体   English

从SQL Server生成JSON

[英]Generate JSON from SQL Server

I need to generate JSON string from a sql table (using the FOR JSON AUTO qualifiers), while one (or more) of the columns is (are) stored as json string. 我需要从sql表(使用FOR JSON AUTO限定符)生成JSON字符串,而其中一列(或多列)存储为json字符串。

eg: 例如:

Table Persons: 表人员:

First_Name    | Family_Name   |City      | Children
--------------+---------------+----------+---------------
David         |Bin            | Miami    |[{"First_Name" :"John","Family_Name":"Bin"}]
Mary          |Nis            | New York |[]

The required result would then be: 所需的结果将是:

[
 {"First_Name":"David",
  "Family_Name":"Bin",
  "City":"Miami",
  "Children": [{"First_Name" :"John",
                "Family_Name":"Bin"}
              ]
 },
 {"First_Name":"Mary",
  "Family_Name":"Nis",
  "City":"New York",
  "Children": []
 }
]

My current issue is that in the result, all the occurrences of the " are escaped and hence the application receiving it fails (illegal JSON). 我当前的问题是,结果中所有出现的"都将转义,因此接收它的应用程序将失败(非法JSON)。

Could anyone suggest the correct phrasing for the SELECT command that would generate the result shown above? 谁能为SELECT命令提供正确的措词,以产生上面显示的结果?

Thanks in advance. 提前致谢。

Try this: 尝试这个:

  SELECT        First_Name      , 
                Family_Name     , 
                City            , 
     JSON_QUERY(Children) 
  FROM Person FOR JSON AUTO ;

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

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