JSON 到 SQL Server 2016 缺少数组中的行
Posted
技术标签:
【中文标题】JSON 到 SQL Server 2016 缺少数组中的行【英文标题】:JSON to SQL Server 2016 missing rows from array 【发布时间】:2018-12-04 06:31:18 【问题描述】:我刚开始将 JSON 导入 SQL Server 2016,我以为我已经搞定了,但我注意到我从数组中遗漏了一些细节,查看图像,有四个地址,但我看到有缺少更多地址,例如 Burrows Rd 和 Urana RD。如何确保所有地址都被捕获?
https://i.stack.imgur.com/erzBV.jpg
@json nvarchar(max)
@json = N'
"response": [
"application":
"info":
"dat_id": "010.2018.00036494.001",
"development_type": "Residential - Single new dwelling",
"application_type": "DA",
"last_modified_date": "2018-12-03T11:35:24+11:00",
"description": "Residence, Garage & Colorbond Shed, Demolition of Existing Residence & Tree Removal",
"authority":
"ref": "http://gemini:82/ApplicationTracker/atdis/1.0",
"name": "AlburyCity"
,
"lodgement_date": "2018-10-26T00:00:00+11:00",
"determination_date": null,
"determination_type": "Pending",
"status": "In Progress",
"notification_start_date": null,
"notification_end_date": null,
"officer": "David Flood",
"estimated_cost": "Not applicable.",
"related_apps": [ ]
,
"reference":
"more_info_url": "http://gemini:82/ApplicationTracker/Application/ApplicationDetails/010.2018.00036494.001/",
"comments_url": null
,
"locations": [
"land_title_ref":
"torrens":
"lot": "11",
"section": null,
"dpsp_id": "DP 1031272"
,
"address":
"street": "680 Centaur RD",
"suburb": "HAMILTON VALLEY",
"state": "NSW",
"postcode": "2641"
,
"geometry": null
,
"land_title_ref":
"torrens":
"lot": "11",
"section": null,
"dpsp_id": "DP 1031272"
,
"address":
"street": "Urana RD",
"suburb": "HAMILTON VALLEY",
"state": "NSW",
"postcode": "2641"
,
"geometry": null
,
"land_title_ref":
"torrens":
"lot": "11",
"section": null,
"dpsp_id": "DP 1031272"
,
"address":
"street": "Burrows RD",
"suburb": "HAMILTON VALLEY",
"state": "NSW",
"postcode": "2641"
,
"geometry": null
],
"events": [
"id": "3680347",
"timestamp": "2018-11-01T15:58:00+11:00",
"description": "Public Notification",
"event_type": "PNOT",
"status": "COMP"
,
"id": "3680348",
"timestamp": "2018-11-01T15:58:00+11:00",
"description": "Referral Engineering",
"event_type": "ENG",
"status": "COMP"
,
"id": "3680349",
"timestamp": "2018-11-01T15:59:00+11:00",
"description": "Referal Parks & Recreation",
"event_type": "TREE",
"status": "COMP"
,
"id": "3680350",
"timestamp": "2018-11-01T16:00:00+11:00",
"description": "Acknowledgement to Applicant",
"event_type": "ACKN",
"status": "COMP"
,
"id": "3683617",
"timestamp": "2018-11-21T14:59:00+11:00",
"description": "Site Assessment Inspection",
"event_type": "SITE",
"status": "PASS"
,
"id": "3685155",
"timestamp": "2018-12-03T11:37:00+11:00",
"description": "Assessment Report",
"event_type": "ASS3",
"status": "COMP"
],
"documents": [
"ref": "DOC18/163129",
"title": "Statement of Environmental Effects - SEE",
"document_url": "http://gemini:82/ApplicationTracker/atdis/1.0/Document/Download?key=6hF7YEiv0qE=&fileName=Statement+of+Environmental+Effects+-+SEE.PDF"
,
"ref": "DOC18/163139",
"title": "Site Plan and Elevations",
"document_url": "http://gemini:82/ApplicationTracker/atdis/1.0/Document/Download?key=1Gv3GVOIHCM=&fileName=Site+Plan+and+Elevations.PDF"
],
"people": [
"name": "Karyn Ford",
"role": "Applicant",
"contact": "6023 8287"
],
"extended":
"software_vendor": "Civica",
"software_vendor_url": "http://www.civica.com.au"
,
"application":
"info":
"dat_id": "017.2018.00036017.001",
"development_type": "Subdivision Only",
"application_type": "SCC",
"last_modified_date": "2018-12-03T10:19:25+11:00",
"description": "Roads, Sewer, Water & Drainage for Two (2) Lot Torrens Title Subdivisi on",
"authority":
"ref": "http://gemini:82/ApplicationTracker/atdis/1.0",
"name": "AlburyCity"
,
"lodgement_date": "2018-11-14T00:00:00+11:00",
"determination_date": "2018-11-29T00:00:00+11:00",
"determination_type": "Approved under delegation",
"status": "Determined",
"notification_start_date": null,
"notification_end_date": null,
"officer": "Sharna Holland",
"estimated_cost": "Not applicable.",
"related_apps": [ "http://gemini:82/ApplicationTracker/atdis/1.0/010.2018.00036017.001.json" ]
,
"reference":
"more_info_url": "http://gemini:82/ApplicationTracker/Application/ApplicationDetails/017.2018.00036017.001/",
"comments_url": null
,
"locations": [
"land_title_ref":
"torrens":
"lot": "A",
"section": null,
"dpsp_id": "DP 161410"
,
"address":
"street": "419 Hovell ST",
"suburb": "SOUTH ALBURY",
"state": "NSW",
"postcode": "2640"
,
"geometry": null
,
"land_title_ref":
"torrens":
"lot": "A",
"section": null,
"dpsp_id": "DP 161410"
,
"address":
"street": "Charles ST",
"suburb": "SOUTH ALBURY",
"state": "NSW",
"postcode": "2640"
,
"geometry": null
],
"events": [
"id": "3683888",
"timestamp": "2018-11-23T14:03:00+11:00",
"description": "Acknowledgement to Applicant",
"event_type": "ACKN",
"status": "COMP"
,
"id": "3683902",
"timestamp": "2018-11-23T15:21:00+11:00",
"description": "Referral Engineering",
"event_type": "ENG",
"status": "COMP"
,
"id": "3683903",
"timestamp": "2018-11-23T15:21:00+11:00",
"description": "Referral Building Surveyor 3",
"event_type": "BS3",
"status": "COMP"
,
"id": "3683904",
"timestamp": "2018-11-23T15:21:00+11:00",
"description": "Referral Trainee Town Planner",
"event_type": "TTP1",
"status": "COMP"
,
"id": "3684791",
"timestamp": "2018-11-29T14:36:00+11:00",
"description": "Collected Determination",
"event_type": "COLL",
"status": "COMP"
],
"documents": [
"ref": "DOC18/177392",
"title": "Subdivision Works Certificate",
"document_url": "http://gemini:82/ApplicationTracker/atdis/1.0/Document/Download?key=8qU1Pkawfvg=&fileName=Subdivision+Works+Certificate.PDF"
],
"people": [
"name": "Eslers Land Consulting",
"role": "Applicant",
"contact": "6021 1322"
],
"extended":
"software_vendor": "Civica",
"software_vendor_url": "http://www.civica.com.au"
,
"application":
"info":
"dat_id": "010.2016.00034838.001",
"development_type": "Residential - New multi unit",
"application_type": "DA",
"last_modified_date": "2018-12-03T09:36:09+11:00",
"description": "Twenty (20) Detached Self Contained Residences - Kensington Gardens Retirement Village",
"authority":
"ref": "http://gemini:82/ApplicationTracker/atdis/1.0",
"name": "AlburyCity"
,
"lodgement_date": "2016-08-17T00:00:00+10:00",
"determination_date": "2016-10-24T00:00:00+11:00",
"determination_type": "Approved under delegation",
"status": "Determined",
"notification_start_date": null,
"notification_end_date": null,
"officer": "Christopher Eldred",
"estimated_cost": "Not applicable.",
"related_apps": [ "http://gemini:82/ApplicationTracker/atdis/1.0/011.2016.00034838.001.json", "http://gemini:82/ApplicationTracker/atdis/1.0/011.2016.00034838.002.json", "http://gemini:82/ApplicationTracker/atdis/1.0/011.2016.00034838.003.json", "http://gemini:82/ApplicationTracker/atdis/1.0/011.2016.00034838.004.json", "http://gemini:82/ApplicationTracker/atdis/1.0/011.2016.00034838.005.json", "http://gemini:82/ApplicationTracker/atdis/1.0/011.2016.00034838.006.json", "http://gemini:82/ApplicationTracker/atdis/1.0/011.2016.00034838.007.json", "http://gemini:82/ApplicationTracker/atdis/1.0/011.2016.00034838.008.json", "http://gemini:82/ApplicationTracker/atdis/1.0/011.2016.00034838.009.json", "http://gemini:82/ApplicationTracker/atdis/1.0/011.2016.00034838.010.json", "http://gemini:82/ApplicationTracker/atdis/1.0/011.2016.00034838.011.json", "http://gemini:82/ApplicationTracker/atdis/1.0/011.2016.00034838.012.json" ]
,
"reference":
"more_info_url": "http://gemini:82/ApplicationTracker/Application/ApplicationDetails/010.2016.00034838.001/",
"comments_url": null
,
"locations": [
"land_title_ref":
"torrens":
"lot": "2",
"section": null,
"dpsp_id": "DP 874732"
,
"address":
"street": "100 Table Top RD",
"suburb": "THURGOONA",
"state": "NSW",
"postcode": "2640"
,
"geometry": null
],
"events": [
"id": "3583145",
"timestamp": "2016-08-17T14:13:00+10:00",
"description": "Further Information Requested",
"event_type": "INFO",
"status": "COMP"
,
"id": "3573096",
"timestamp": "2016-08-18T15:34:00+10:00",
"description": "Public Notification",
"event_type": "PNOT",
"status": "COMP"
,
"id": "3573097",
"timestamp": "2016-08-18T15:37:00+10:00",
"description": "Referral Building Surveyor 2",
"event_type": "BS2",
"status": "COMP"
,
"id": "3573098",
"timestamp": "2016-08-18T15:37:00+10:00",
"description": "Referral Plumbing Inspector",
"event_type": "PI",
"status": "COMP"
,
"id": "3573099",
"timestamp": "2016-08-18T15:37:00+10:00",
"description": "Referral Engineering",
"event_type": "ENG",
"status": "COMP"
,
"id": "3573100",
"timestamp": "2016-08-18T15:38:00+10:00",
"description": "Referral Environmental Planner",
"event_type": "ENV",
"status": "COMP"
,
"id": "3573103",
"timestamp": "2016-08-18T15:43:43+10:00",
"description": "Acknowledgement to Applicant",
"event_type": "ACKN",
"status": "COMP"
,
"id": "3575194",
"timestamp": "2016-09-06T00:00:00+10:00",
"description": "Assessment Report",
"event_type": "ASS3",
"status": "COMP"
],
"documents": [
"ref": "DOC16/209893",
"title": "Statement of Environmental Effects - SEE",
"document_url": "http://gemini:82/ApplicationTracker/atdis/1.0/Document/Download?key=sQkVV9rEsTU=&fileName=Statement+of+Environmental+Effects+-+SEE.PDF"
,
"ref": "DOC16/209896",
"title": "Site Plan & Elevations",
"document_url": "http://gemini:82/ApplicationTracker/atdis/1.0/Document/Download?key=3dRqEZHzGeo=&fileName=Site+Plan+%26+Elevations.PDF"
,
"ref": "DOC16/211819",
"title": "Assessment Report",
"document_url": "http://gemini:82/ApplicationTracker/atdis/1.0/Document/Download?key=DVTQkQQqbns=&fileName=Assessment+Report.PDF"
,
"ref": "DOC16/240764",
"title": "Development Consent",
"document_url": "http://gemini:82/ApplicationTracker/atdis/1.0/Document/Download?key=TY+Y3zjyDKw=&fileName=Development+Consent.PDF"
],
"people": [
"name": "Kensington Gardens Lifestyle Estates",
"role": "Applicant",
"contact": "6049 3100"
],
"extended":
"software_vendor": "Civica",
"software_vendor_url": "http://www.civica.com.au"
,
"application":
"info":
"dat_id": "010.2018.00036468.001",
"development_type": "Residential - Single new dwelling",
"application_type": "DA",
"last_modified_date": "2018-11-30T17:17:25+11:00",
"description": "Residence, Garage and Retaining Walls",
"authority":
"ref": "http://gemini:82/ApplicationTracker/atdis/1.0",
"name": "AlburyCity"
,
"lodgement_date": "2018-10-19T00:00:00+11:00",
"determination_date": "2018-11-26T00:00:00+11:00",
"determination_type": "Approved under delegation",
"status": "Determined",
"notification_start_date": null,
"notification_end_date": null,
"officer": "David Flood",
"estimated_cost": "Not applicable.",
"related_apps": [ "http://gemini:82/ApplicationTracker/atdis/1.0/011.2018.00036468.001.json" ]
,
"reference":
"more_info_url": "http://gemini:82/ApplicationTracker/Application/ApplicationDetails/010.2018.00036468.001/",
"comments_url": null
,
"locations": [
"land_title_ref":
"torrens":
"lot": "218",
"section": null,
"dpsp_id": "DP 1228226"
,
"address":
"street": "20 Stockman CRCT",
"suburb": "THURGOONA",
"state": "NSW",
"postcode": "2640"
,
"geometry": null
],
"events": [
"id": "3678966",
"timestamp": "2018-10-25T10:47:00+11:00",
"description": "Public Notification",
"event_type": "PNOT",
"status": "COMP"
,
"id": "3678967",
"timestamp": "2018-10-25T10:48:00+11:00",
"description": "Referral Engineering",
"event_type": "ENG",
"status": "COMP"
,
"id": "3678974",
"timestamp": "2018-10-25T10:51:00+11:00",
"description": "Acknowledgement to Applicant",
"event_type": "ACKN",
"status": "COMP"
,
"id": "3681955",
"timestamp": "2018-11-01T15:49:00+11:00",
"description": "Site Assessment Inspection",
"event_type": "SITE",
"status": "COMP"
,
"id": "3684251",
"timestamp": "2018-11-27T10:24:00+11:00",
"description": "Collected Determination",
"event_type": "COLL",
"status": "COMP"
],
"documents": [
"ref": "DOC18/159026",
"title": "Statement of Environmental Effects - SEE",
"document_url": "http://gemini:82/ApplicationTracker/atdis/1.0/Document/Download?key=MaiWkTs8V+g=&fileName=Statement+of+Environmental+Effects+-+SEE.PDF"
,
"ref": "DOC18/159033",
"title": "Site Plan and Elevations and Superseded Plan",
"document_url": "http://gemini:82/ApplicationTracker/atdis/1.0/Document/Download?key=2xpqra8gNb0=&fileName=Site+Plan+and+Elevations+and+Superseded+Plan.PDF"
,
"ref": "DOC18/162569",
"title": "Assessment Report - Bldg Residential",
"document_url": "http://gemini:82/ApplicationTracker/atdis/1.0/Document/Download?key=bKYCMP01aJE=&fileName=Assessment+Report+-+Bldg+Residential.PDF"
,
"ref": "DOC18/168584",
"title": "Development Consent",
"document_url": "http://gemini:82/ApplicationTracker/atdis/1.0/Document/Download?key=SxGG/yKi68s=&fileName=Development+Consent.PDF"
],
"people": [
"name": "Alatalo Bros",
"role": "Applicant",
"contact": "02 6055 0180"
],
"extended":
"software_vendor": "Civica",
"software_vendor_url": "http://www.civica.com.au"
],
"count": 4,
"pagination":
"previous": null,
"next": 2,
"current": 1,
"per_page": 4,
"count": 24091,
"pages": 6023
'
select *
from OPENJSON(@json, '$.response')
with
([lot] varchar(200) '$.application.locations[0].land_title_ref.torrens.lot',
[section] varchar(200) '$.application.locations[0].land_title_ref.torrens.section',
[dpsp_id] varchar(200) '$.application.locations[0].land_title_ref.torrens.dpsp_id',
[Street] varchar(200) '$.application.locations[0].address.street',
[suburb] varchar(200) '$.application.locations[0].address.suburb',
[state] varchar(200) '$.application.locations[0].address.state',
[postcode] varchar(200) '$.application.locations[0].address.postcode',
[geometry] varchar(200) '$.application.locations[0].geometry'
)
【问题讨论】:
【参考方案1】:试试这个:
select L.*
from OPENJSON(@json,'$.response') R
CROSS APPLY OPENJSON(R.[value], '$.application.locations')
with
(
[lot] varchar(200) '$.land_title_ref.torrens.lot',
[section] varchar(200) '$.land_title_ref.torrens.section',
[dpsp_id] varchar(200) '$.land_title_ref.torrens.dpsp_id',
[Street] varchar(200) '$.address.street',
[suburb] varchar(200) '$.address.suburb',
[state] varchar(200) '$.address.state',
[postcode] varchar(200) '$.address.postcode',
[geometry] varchar(200) '$.geometry'
) L
locations
是一个数组,所以我们需要使用cross apply和OPENSJON来获取所有元素。
【讨论】:
完美!非常感谢,也意味着我不用打字了,在这里学到了新东西,快乐的日子! 实际上我有一个后续问题,假设我需要包含第二个数组?就像这里的最后两行?我尝试放入另一个 CROSS APPLY 并为其加上别名,但它不喜欢它,我还在 $ 之后尝试了一些不同的组合。但也没有工作。我不想只为最后两行创建另一个表。我怎么能包括“参考”? ( [related_apps] varchar(200) '$.related_apps', [more_info_url] varchar(200) '$.application.reference.more_info_url', [cmets_url] varchar(200) '$.application.reference.cmets_url' )L以上是关于JSON 到 SQL Server 2016 缺少数组中的行的主要内容,如果未能解决你的问题,请参考以下文章
使用 C# 和 OPENJSON 将 JSON 插入 SQL Server 2016