SQL Server导入JSON子组数组

发布于 2025-02-14 02:22:02 字数 1782 浏览 2 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮邮递员 2025-02-21 02:22:02

您需要使用 作为JSON选项,然后您可以在需要时进一步查询,以将其转换为一个通过 <<<代码> string_agg() ,例如:

declare @json nvarchar(max) =
N'{
    "top": 19.2743,
    "bottom": 20.3115,
    "left": 0.2878,
    "right": 1.7038,
    "isInternalToDevice": false,
    "numberOfLegs": 3,
    "legs": [
        3,
        2
    ]
}';

select *
from openjson(@json) with (
    [NumberOfLegs] int N'$.numberOfLegs',
    [Legs] nvarchar(max) N'$.legs' as JSON
) shredded
outer apply (
    select string_agg(value, ',') from openjson(Legs)
) joined(Leggy);

返回结果:

numberFlegs
3[3,2]3,2

You'll need to return $.legs using the AS JSON option and then you can query that further, if needed, to transform it into a comma-delimited string by way of STRING_AGG(), e.g.:

declare @json nvarchar(max) =
N'{
    "top": 19.2743,
    "bottom": 20.3115,
    "left": 0.2878,
    "right": 1.7038,
    "isInternalToDevice": false,
    "numberOfLegs": 3,
    "legs": [
        3,
        2
    ]
}';

select *
from openjson(@json) with (
    [NumberOfLegs] int N'$.numberOfLegs',
    [Legs] nvarchar(max) N'$.legs' as JSON
) shredded
outer apply (
    select string_agg(value, ',') from openjson(Legs)
) joined(Leggy);

Which returns the results:

NumberOfLegsLegsLeggy
3[ 3, 2 ]3,2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文