SQL Server导入JSON子组数组
我有一个SQL Server脚本的代码摘录,该脚本将读取JSON文件并插入表中。 我被文件的一部分陷入困境。示例:
"top": 19.2743,
"bottom": 20.3115,
"left": 0.2878,
"right": 1.7038,
"isInternalToDevice": false,
"numberOfLegs": 3,
"legs": [
3,
2
],
我试图获得“腿”,因为“ 3,2”
这是读取其他所有内容的代码 - 只需要腿(站在:-))
DECLARE @JSONRoot VARCHAR(50)
SET @JSONRoot = '$._embedded.symbols'
SELECT
[id],
[description],
[displayCategoryProgrammaticName],
[displayCategoryProgrammaticNameDisplay],
[manufacturer],
[model],
[modelqualifier],
[ProgrammaticName],
[Type],
[Position],
[Label],
[ReceptacleType],
[ConnectorType],
[NumberOfLegs]
FROM OPENROWSET (BULK 'D:\Test\TestFileSymbols.json', SINGLE_CLOB) as j
CROSS APPLY OPENJSON(BulkColumn, ''+@JSONRoot+'')
WITH
(
[id] UNIQUEIDENTIFIER,
[description] VARCHAR(200),
[displayCategoryProgrammaticName] VARCHAR(50),
[displayCategoryProgrammaticNameDisplay] VARCHAR(50),
[manufacturer] VARCHAR(100),
[model] VARCHAR(100),
[modelqualifier] VARCHAR(100),
[openings] NVARCHAR(MAX)'$.openings' AS JSON
)
OUTER APPLY OPENJSON(openings)
WITH (
[ProgrammaticName] VARCHAR(100) N'$.programmaticName',
[Type] VARCHAR(100) N'$.type',
[Position] VARCHAR(100) N'$.side',
[Label] VARCHAR(30) N'$.label',
[ReceptacleType] VARCHAR(100) N'$.receptacleType',
[ConnectorType] VARCHAR(50) N'$.connectorType',
[NumberOfLegs] INT N'$.numberOfLegs',
JSON_QUERY([openings], N'$.legs') AS Legs
)
I have a code excerpt of a SQL Server script that reads the JSON files and inserts into a table.
I am stuck with a section of the file that is an array. Example:
"top": 19.2743,
"bottom": 20.3115,
"left": 0.2878,
"right": 1.7038,
"isInternalToDevice": false,
"numberOfLegs": 3,
"legs": [
3,
2
],
I am trying to get the "legs, as "3,2"
Here is the code that reads everything else - just need the legs (to stand on :-) )
DECLARE @JSONRoot VARCHAR(50)
SET @JSONRoot = '$._embedded.symbols'
SELECT
[id],
[description],
[displayCategoryProgrammaticName],
[displayCategoryProgrammaticNameDisplay],
[manufacturer],
[model],
[modelqualifier],
[ProgrammaticName],
[Type],
[Position],
[Label],
[ReceptacleType],
[ConnectorType],
[NumberOfLegs]
FROM OPENROWSET (BULK 'D:\Test\TestFileSymbols.json', SINGLE_CLOB) as j
CROSS APPLY OPENJSON(BulkColumn, ''+@JSONRoot+'')
WITH
(
[id] UNIQUEIDENTIFIER,
[description] VARCHAR(200),
[displayCategoryProgrammaticName] VARCHAR(50),
[displayCategoryProgrammaticNameDisplay] VARCHAR(50),
[manufacturer] VARCHAR(100),
[model] VARCHAR(100),
[modelqualifier] VARCHAR(100),
[openings] NVARCHAR(MAX)'$.openings' AS JSON
)
OUTER APPLY OPENJSON(openings)
WITH (
[ProgrammaticName] VARCHAR(100) N'$.programmaticName',
[Type] VARCHAR(100) N'$.type',
[Position] VARCHAR(100) N'$.side',
[Label] VARCHAR(30) N'$.label',
[ReceptacleType] VARCHAR(100) N'$.receptacleType',
[ConnectorType] VARCHAR(50) N'$.connectorType',
[NumberOfLegs] INT N'$.numberOfLegs',
JSON_QUERY([openings], N'$.legs') AS Legs
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
作为JSON
选项,然后您可以在需要时进一步查询,以将其转换为一个通过 <<<代码> string_agg() ,例如:返回结果:
You'll need to return
$.legs
using theAS JSON
option and then you can query that further, if needed, to transform it into a comma-delimited string by way ofSTRING_AGG()
, e.g.:Which returns the results: