我希望有人能帮我指出正确的方向,从下面的JSON例子中解析数组对象。目前,我能够正确地解析出大部分的JSON。
我遇到的问题是将parentId数组拉到单独的行中,绑定到主ID字段。
下面是JSON的一个示例。
{
"kind": "folders",
"data": [
{
"id": "IEABQ5EAI4KGTYGQ",
"accountId": "IEABQ5EA",
"title": "test prj",
"createdDate": "2019-04-17T20:39:58Z",
"updatedDate": "2020-01-16T22:49:09Z",
"description": "",
"sharedIds": [
"KX74YUWR",
"KUAD47VS",
"KX75JYTL",
"KUAEH2GT",
"KUAERUOG",
"KX75EJSV",
"KX75JYTH",
"KUAEPBXA",
"KX74ZXJC",
"KUADJ7OW",
"KX75JYTF",
"KX75JQE2",
"KX75JYTA",
"KUAC6PRX",
"KUAD2ZGS",
"KUADJYXY",
"KX75JYS5",
"KUADQEMC",
"KUADJXDI",
"KUADAEPD",
"KX732DQC",
"KUACJOOM"
],
"parentIds": [
"IEABQ5EAI4KGTXVG"
],
"childIds": [],
"scope": "WsFolder",
"permalink": "www.test.com",
"workflowId": "IEABQ5EAK776PC4A",
"customFields": [],
"customColumnIds": [],
"project": {
"authorId": "KUADJXDI",
"ownerIds": [
"KUADJXDI"
],
"status": "Completed",
"customStatusId": "IEABQ5EAJMA6STFV",
"startDate": "2019-05-29",
"endDate": "2019-06-18",
"createdDate": "2019-05-31T04:56:58Z",
"completedDate": "2020-01-16T22:49:09Z"
}
}
]
}
下面的SQL代码是我用来解析出主要字段的代码--
SELECT [key] AS Doc_id, id, title, createdDate, updatedDate, [description], permalink, workflowId,
parentIds, customFields, customColumnIds, project
into #JsonDocTemp
FROM OPENJSON (@WJSON2, '$.data') AS EachDoc
CROSS APPLY OPENJSON(EachDoc.Value)
WITH (
id NVARCHAR(20) N'$.id',
title NVARCHAR(500) N'$.title',
createdDate DATETIME '$.createdDate',
updatedDate DATETIME '$.updatedDate',
[description] NVARCHAR(MAX) N'$.description',
permalink NVARCHAR(100) N'$.permalink',
workflowId NVARCHAR(20) N'$.workflowId',
parentIds NVARCHAR(MAX) N'$.parentIds' AS JSON,
customFields NVARCHAR(MAX) N'$.customFields' AS JSON,
customColumnIds NVARCHAR(MAX) N'$.customColumnIds' AS JSON,
project NVARCHAR(MAX) N'$.parentIds' AS JSON
)
我想将临时表中的parentId解析到行中,或者使用.data直接从@WJSON2解析parentId。parentId也可以。
任何帮助-我想我快到了,但我错过了一些东西。
谢谢杰里米
1条答案
按热度按时间fgw7neuy1#
对于每个嵌套级别,您需要使用附加的
APPLY
运算符和带有显式模式的OPENJSON()
调用:Json:
语句(从每个JSON级别仅返回
id
和parentIds
):结果:
语句(解析其他JSON键和嵌套数组):