OPENJSON - Невозможно запросить вложенные элементы - PullRequest
2 голосов
/ 14 мая 2019

Мне трудно запрашивать вложенные элементы документа JSON с помощью функции T-SQL OPENJSON в SQL Server 2016. Я читаю документы MSDN здесь , здесь & здесь пока не нашли правильный синтаксис для блока WITH.Мои столбцы продолжают возвращать только одну пустую строку.

Может ли кто-нибудь помочь с модификацией T-SQL для получения запроса, подобного этому (ниже)?

enter image description here

Вот структура json и нулевой вывод:

DECLARE @json NVARCHAR(MAX)
SET @json =  N'{"meta":{"total_record_count":4870,"total_pages":98,"current_page":97,"per_page":50},"companies":[{"id":"13463591","url":"https://api.mattermark.com/companies/13463591","company_name":"Modula4","domain":"modula4.com"},{"id":"13531979","url":"https://api.mattermark.com/companies/13531979","company_name":"Rae Development Corp","domain":"raedevelopment.com"},{"id":"13537660","url":"https://api.mattermark.com/companies/13537660","company_name":"jBASE","domain":"jbase.com"},{"id":"13537769","url":"https://api.mattermark.com/companies/13537769","company_name":"D2Effects LLC","domain":"bitefx.com"},{"id":"13537784","url":"https://api.mattermark.com/companies/13537784","company_name":"eSyncTraining","domain":"esynctraining.com"},{"id":"13541751","url":"https://api.mattermark.com/companies/13541751","company_name":"Business Computing","domain":"bcinc.net"},{"id":"13553270","url":"https://api.mattermark.com/companies/13553270","company_name":"KNIGHTLING INC.","domain":"knightling.com"},{"id":"13560476","url":"https://api.mattermark.com/companies/13560476","company_name":"Netcentric systems","domain":"netcentricsystems.com"},{"id":"13560643","url":"https://api.mattermark.com/companies/13560643","company_name":"Fortium Solutions, LLC","domain":"fortiumsolutions.com"},{"id":"13561893","url":"https://api.mattermark.com/companies/13561893","company_name":"PMAC SERVICES INC","domain":"pmacservices.com"},{"id":"13587256","url":"https://api.mattermark.com/companies/13587256","company_name":"Bayonet Inc.","domain":"mybayonet.com"},{"id":"13590982","url":"https://api.mattermark.com/companies/13590982","company_name":"IQA","domain":"instrumentalqa.com"},{"id":"13593143","url":"https://api.mattermark.com/companies/13593143","company_name":"Atech Consultants","domain":"atechconsultants.com"},{"id":"13601156","url":"https://api.mattermark.com/companies/13601156","company_name":"Csoft Corp","domain":"csoftcorp.net"},{"id":"13602832","url":"https://api.mattermark.com/companies/13602832","company_name":"Gareth, Inc.","domain":"garethinc.com"},{"id":"13604890","url":"https://api.mattermark.com/companies/13604890","company_name":"Xpaseo","domain":"xpaseo.com"},{"id":"13610146","url":"https://api.mattermark.com/companies/13610146","company_name":"Imagine IT","domain":"imagineit.com"},{"id":"13610923","url":"https://api.mattermark.com/companies/13610923","company_name":"HTS Solutions Pvt Ltd","domain":"htssolutions.org"},{"id":"13619836","url":"https://api.mattermark.com/companies/13619836","company_name":"Tgm Software","domain":"tgmsoftware.com"},{"id":"13622956","url":"https://api.mattermark.com/companies/13622956","company_name":"Inaspan LLC","domain":"inaspan.com"},{"id":"13627130","url":"https://api.mattermark.com/companies/13627130","company_name":"Forerunner Systems Inc","domain":"forerunnersystems.com"},{"id":"13628312","url":"https://api.mattermark.com/companies/13628312","company_name":"Advanced Integrated Solutions, Inc.","domain":"aisconsulting.net"},{"id":"13629029","url":"https://api.mattermark.com/companies/13629029","company_name":"Pounce Consulting","domain":"pouncecorp.com"},{"id":"13629537","url":"https://api.mattermark.com/companies/13629537","company_name":"CloudServe Corporation","domain":"cloudservecorp.com"},{"id":"13635102","url":"https://api.mattermark.com/companies/13635102","company_name":"Leivio Technologies","domain":"leivio.com"},{"id":"13636749","url":"https://api.mattermark.com/companies/13636749","company_name":"Alpen Technology Group","domain":"alpentg.com"},{"id":"13636754","url":"https://api.mattermark.com/companies/13636754","company_name":"National Schedule Masters","domain":"tractime.com"},{"id":"13645065","url":"https://api.mattermark.com/companies/13645065","company_name":"RISA Technologies","domain":"risa.com"},{"id":"13646467","url":"https://api.mattermark.com/companies/13646467","company_name":"Enterprise Answers, LLC","domain":"enterpriseanswers.com"},{"id":"13647801","url":"https://api.mattermark.com/companies/13647801","company_name":"Office Automation Centers Inc","domain":"officeautomationcenter.com"},{"id":"13650810","url":"https://api.mattermark.com/companies/13650810","company_name":"Mozgomedia","domain":"mozgomedia.com"},{"id":"13652763","url":"https://api.mattermark.com/companies/13652763","company_name":"2000 Networking","domain":"network2000-hi.com"},{"id":"13654103","url":"https://api.mattermark.com/companies/13654103","company_name":"Vega Imaging","domain":"vega-imaging.com"},{"id":"13654874","url":"https://api.mattermark.com/companies/13654874","company_name":"Astha Inc.","domain":"asthainc.com"},{"id":"13656103","url":"https://api.mattermark.com/companies/13656103","company_name":"Management Applied Programming","domain":"mapinc.com"},{"id":"13657848","url":"https://api.mattermark.com/companies/13657848","company_name":"4Hilton Inc.","domain":"4hilton.com"},{"id":"13658020","url":"https://api.mattermark.com/companies/13658020","company_name":"FCC Group","domain":"fccgroup.com"},{"id":"13658316","url":"https://api.mattermark.com/companies/13658316","company_name":"Wavelength Datacom, Inc.","domain":"wavdata.com"},{"id":"13661074","url":"https://api.mattermark.com/companies/13661074","company_name":"Xintex Corporation","domain":"xintex.com"},{"id":"13663085","url":"https://api.mattermark.com/companies/13663085","company_name":"DCL Media Services","domain":"onlinecopycorp.com"},{"id":"13667881","url":"https://api.mattermark.com/companies/13667881","company_name":"LMSPros","domain":"lmspros.com"},{"id":"13669354","url":"https://api.mattermark.com/companies/13669354","company_name":"Sujansky \u0026 Associates, LLC","domain":"sujansky.com"},{"id":"13669681","url":"https://api.mattermark.com/companies/13669681","company_name":"Felosoft LLC","domain":"felosoft.com"},{"id":"13669759","url":"https://api.mattermark.com/companies/13669759","company_name":"Collaborative Technologies Inc","domain":"collaborationtech.net"},{"id":"13670474","url":"https://api.mattermark.com/companies/13670474","company_name":"Freight Stream Inc","domain":"freightstream.com"},{"id":"13675681","url":"https://api.mattermark.com/companies/13675681","company_name":"eDimension Consulting","domain":"edimensionconsulting.com"},{"id":"13676806","url":"https://api.mattermark.com/companies/13676806","company_name":"LTT Net Solutions","domain":"lttnetsolutions.com"},{"id":"13678223","url":"https://api.mattermark.com/companies/13678223","company_name":"OCRTI Consulting Corp","domain":"ocrti.com"},{"id":"13679946","url":"https://api.mattermark.com/companies/13679946","company_name":"Mac Networks","domain":"macnetworks.com"},{"id":"13681009","url":"https://api.mattermark.com/companies/13681009","company_name":"NetMost Web Technologies","domain":"netmostwebdesign.com"}],"total_companies":4870,"page":97,"per_page":50}'

SELECT * 
FROM OPENJSON(@json)
WITH (   
       id     int '$.companies.id' ,  
       [url]  nvarchar(max) '$.companies.url',  
       company_name nvarchar(max) '$.companies.company_name',  
       domain nvarchar(max)'$.companies.domain'  
     )

enter image description here

Ответы [ 3 ]

0 голосов
/ 14 мая 2019

Если вы хотите получить все свои данные JSON в качестве набора результатов, вам нужен дополнительный оператор APPLY со вторым вызовом OPENJSON() (для вложенного массива JSON), используя явное определение схемы:

T-SQL:

SELECT 
   j.total_companies, j.page, j.per_page,
   a.*
FROM OPENJSON(@json)
WITH (   
   total_companies int '$.total_companies',
   page int '$.page',
   per_page int '$.per_page',
   companies nvarchar(max) '$.companies' AS JSON
) j
CROSS APPLY OPENJSON(j.companies) 
WITH (
   id nvarchar(10) '$.id',
   url nvarchar(max) '$.url',
   company_name nvarchar(100) '$.company_name',
   domain nvarchar(max) '$.domain'
) a

Вывод (только первые 10 записей из 50):

total_companies page    per_page    id          url                                             company_name            domain
4870            97      50          13463591    https://api.mattermark.com/companies/13463591   Modula4                 modula4.com
4870            97      50          13531979    https://api.mattermark.com/companies/13531979   Rae Development Corp    raedevelopment.com
4870            97      50          13537660    https://api.mattermark.com/companies/13537660   jBASE                   jbase.com
4870            97      50          13537769    https://api.mattermark.com/companies/13537769   D2Effects LLC           bitefx.com
4870            97      50          13537784    https://api.mattermark.com/companies/13537784   eSyncTraining           esynctraining.com
4870            97      50          13541751    https://api.mattermark.com/companies/13541751   Business Computing      bcinc.net
4870            97      50          13553270    https://api.mattermark.com/companies/13553270   KNIGHTLING INC.         knightling.com
4870            97      50          13560476    https://api.mattermark.com/companies/13560476   Netcentric systems      netcentricsystems.com
4870            97      50          13560643    https://api.mattermark.com/companies/13560643   Fortium Solutions, LLC  fortiumsolutions.com
4870            97      50          13561893    https://api.mattermark.com/companies/13561893   PMAC SERVICES INC       pmacservices.com
0 голосов
/ 14 мая 2019

Это довольно просто ...

Ваш JSON состоит из двух объектов, первый из которых meta и, похоже, 1:1 связан с контентом.Это лучше всего делать, используя прямые вызовы JSON_VALUE().

Второй, companies - это вложенный JSON-массив.Попробуйте это (сокращенно для краткости)

DECLARE @json NVARCHAR(MAX)
SET @json =  N'{"meta":{"total_record_count":4870,"total_pages":98,"current_page":97,"per_page":50},"companies":[{"id":"13463591","url":"https://api.mattermark.com/companies/13463591","company_name":"Modula4","domain":"modula4.com"},{"id":"13531979","url":"https://api.mattermark.com/companies/13531979","company_name":"Rae Development Corp","domain":"raedevelopment.com"},{"id":"13537660","url":"https://api.mattermark.com/companies/13537660","company_name":"jBASE","domain":"jbase.com"}]}'

- это возвращает meta значения

SELECT JSON_VALUE(@json,'$.meta.total_record_count') AS total_record_count
        ,JSON_VALUE(@json,'$.meta.total_pages') AS total_pages
        ,JSON_VALUE(@json,'$.meta.current_page') AS current_page
        ,JSON_VALUE(@json,'$.meta.per_page') AS per_page

- и это возвращает массив companies:

SELECT A.*
FROM OPENJSON(@json,'$.companies')
WITH (   
       id     int ,  
       [url]  nvarchar(max),  
       company_name nvarchar(max),  
       domain nvarchar(max)  
     ) A;

Подсказка: при необходимости вы можете объединить их с CROSS JOIN ...

0 голосов
/ 14 мая 2019

Компаний это массив в вашем случае. Номер элемента массива должен быть выбран для просмотра данных:

SELECT * 
FROM OPENJSON(@json)
WITH (   
       id     INT '$.companies[0].id' ,  
       [url]  NVARCHAR(MAX) '$.companies[0].url',  
       company_name NVARCHAR(MAX) '$.companies[0].company_name',  
       domain NVARCHAR(MAX) '$.companies[0].domain'  
     )
;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...