如何将JSON从文件加载到SQL Server 2016?

时间:2016-07-14 19:41:50

标签: json openrowset sql-server-2016

我试图将数据从JSON格式移动到SQl Server。我'使用SQL Server 2016,因为此版本支持使用OPENROWSET导入JSON数据。

我使用以下内容查询/导入数据,但我需要一些帮助来解析内容。下面是我的查询和我试图移动到DB的JSON示例文件。

查询

 SELECT value
 FROM OPENROWSET (BULK 'C:\temp\temp.json', SINGLE_CLOB) as j
 CROSS APPLY OPENJSON(BulkColumn)

对于上面的内容,我得到的结果是2行,其中我的第一行是一些我不需要的信息。我想知道,我怎么能只选择我的第二行。

这是JSON文件

{
  "meta": {
    "disclaimer": "Do not rely on openFDA to make decisions regarding medical care. While we make every effort to ensure that data is accurate, you should assume all results are unvalidated. We may limit or otherwise restrict your access to the API in line with our Terms of Service.",
    "terms": "https://open.fda.gov/terms/",
    "license": "https://open.fda.gov/license/",
    "last_updated": "2016-05-28",
    "results": {
      "skip": 0,
      "limit": 1,
      "total": 1540390
    }
  },


  "results": [
    {
      "manufacturer_contact_zip_ext": "",
      "manufacturer_g1_address_2": "",
      "event_location": "",
      "report_to_fda": "Y",
      "manufacturer_contact_t_name": "",
      "manufacturer_contact_state": "",
      "manufacturer_link_flag": "Y",
      "manufacturer_g1_city": "ZUG",
      "manufacturer_contact_address_2": "",
      "event_type": "Malfunction",
      "manufacturer_contact_pcity": "",
      "manufacturer_contact_address_1": "GUBELSTRASSE 34",
      "report_number": "3008382007-2012-09245",
      "type_of_report": [
        "Initial submission"
      ],
      "product_problem_flag": "Y",
      "date_received": "20130101",
      "manufacturer_address_2": "",
      "reprocessed_and_reused_flag": "N",
      "manufacturer_address_1": "",
      "manufacturer_contact_zip_code": "6300",
      "manufacturer_contact_plocal": "",
      "reporter_occupation_code": "PATIENT",
      "manufacturer_contact_l_name": "WANDOLSKI",
      "source_type": [
        "Consumer"
      ],
      "distributor_zip_code_ext": "",
      "manufacturer_g1_postal_code": "6300",
      "manufacturer_g1_state": "",
      "date_facility_aware": "20121210",
      "manufacturer_contact_area_code": "",
      "manufacturer_contact_f_name": "YANNICK",
      "previous_use_code": "I",
      "device": [
        {
          "manufacturer_d_address_1": "GUBELSTRASSE 34",
          "manufacturer_d_address_2": "",
          "device_event_key": "",
          "device_sequence_number": " 1.0",
          "manufacturer_d_state": "",
          "manufacturer_d_zip_code": "6300",
          "manufacturer_d_city": "ZUG",
          "lot_number": "3180296",
          "manufacturer_d_postal_code": "6300",
          "manufacturer_d_zip_code_ext": "",
          "model_number": "",
          "date_received": "20130101",
          "device_report_product_code": "NBW",
          "device_operator": "LAY USER/PATIENT",
          "device_availability": "No",
          "other_id_number": "",
          "generic_name": "GLUCOSE MONITORING SYS/KIT",
          "manufacturer_d_name": "LIFESCAN EUROPE, A DIVISION OF CILAG GMBH INTL",
          "manufacturer_d_country": "SZ",
          "brand_name": "OT ULTRALINK METER",
          "openfda": {
            "device_name": "System, Test, Blood Glucose, Over The Counter",
            "medical_specialty_description": "Clinical Chemistry",
            "device_class": "2",
            "regulation_number": "862.1345"
          },
          "device_age_text": "",
          "device_evaluated_by_manufacturer": "R",
          "catalog_number": "",
          "implant_flag": "",
          "date_removed_flag": ""
        }
      ],
      "manufacturer_zip_code": "",
      "manufacturer_contact_country": "SZ",
      "health_professional": "N",
      "manufacturer_g1_zip_code_ext": "",
      "manufacturer_city": "",
      "manufacturer_contact_extension": "",
      "manufacturer_contact_phone_number": "",
      "patient": [
        {
          "sequence_number_treatment": [
            ""
          ],
          "patient_sequence_number": "1",
          "date_received": "20130101",
          "sequence_number_outcome": [
            ""
          ]
        }
      ],
      "distributor_city": "",
      "distributor_state": "",
      "date_report": "20121210",
      "initial_report_to_fda": "Unknown",
      "manufacturer_g1_country": "SZ",
      "event_key": "",
      "manufacturer_contact_city": "ZUG",
      "mdr_report_key": "2891923",
      "removal_correction_number": "",
      "number_devices_in_event": "",
      "date_manufacturer_received": "20121210",
      "manufacturer_name": "",
      "report_source_code": "Manufacturer report",
      "remedial_action": [
        ""
      ],
      "manufacturer_g1_zip_code": "6300",
      "manufacturer_zip_code_ext": "",
      "report_to_manufacturer": "",
      "manufacturer_g1_name": "LIFESCAN EUROPE, A DIVISION OF CILAG GMBH INTL",
      "distributor_address_1": "",
      "adverse_event_flag": "N",
      "manufacturer_state": "",
      "distributor_address_2": "",
      "manufacturer_postal_code": "",
      "manufacturer_country": "",
      "single_use_flag": "N",
      "mdr_text": [
        {
          "mdr_text_key": "16750885",
          "text_type_code": "Description of Event or Problem",
          "patient_sequence_number": "1",
          "text": "ON (B)(6) 2012, THE LAY USER/ PATIENT CONTACTED LIFESCAN (LFS) IN USA ALLEGING AN ER 2 ISSUE. THE PATIENT DID NOT ALLEGE ANY HARM OR INJURY DUE TO THE ALLEGED ISSUE. THE ALLEGED ISSUE WAS NOT RESOLVED WITH TROUBLESHOOTING. BASED ON THE INFORMATION PROVIDED, THERE IS NO INDICATION THAT THE REPORTED ISSUE CAUSED OR CONTRIBUTED TO A SERIOUS INJURY. THE PATIENT DID NOT DEVELOP SYMPTOMS SUGGESTIVE OF SEVERE HYPOGLYCEMIA OR HYPERGLYCEMIA, NOR RECEIVE MEDICAL INTERVENTION FOR EITHER OF THESE CONDITIONS. HOWEVER, THIS COMPLAINT IS BEING REPORTED BECAUSE THE ALLEGED PRODUCT ISSUE REMAINED UNRESOLVED."
        }
      ],
      "number_patients_in_event": "",
      "distributor_name": "",
      "manufacturer_g1_address_1": "GUBELSTRASSE 34",
      "distributor_zip_code": "",
      "manufacturer_contact_exchange": "",
      "manufacturer_contact_postal_code": "6300",
      "manufacturer_contact_pcountry": ""
    }
  ]
}

1 个答案:

答案 0 :(得分:4)

当您说"第二行"时,我假设您想要数据"结果"宾语。 OPENJSON为每个键返回一行:在输入中找到的值对(仅在输入对象的第一级)。在你的情况下,你有两个对象,一个在" meta"关键,另一个"结果"键。

您可以使用[key]列过滤第二行:

SELECT value
FROM OPENROWSET (BULK 'C:\temp\temp.json', SINGLE_CLOB) as j
CROSS APPLY OPENJSON(BulkColumn)
WHERE [key] = 'results'

或者您可以在路径$ .result中搜索JSON并获取此对象中的所有节点:

SELECT value
FROM OPENROWSET (BULK 'C:\temp\temp.json', SINGLE_CLOB) as j
CROSS APPLY OPENJSON(BulkColumn, '$.results')

也许你也可以看看OPENJSON WITH模式,它可以帮助你将JSON对象中的节点转换为单元格,例如:

SELECT *
FROM OPENROWSET (BULK 'C:\temp\temp.json', SINGLE_CLOB) as j
CROSS APPLY OPENJSON(BulkColumn, '$.results')
WITH (report_number nvarchar(400), date_received nvarchar(400),device nvarchar(max) AS JSON)

如果指定NVARCHAR(MAX)AS JSON作为返回类型,则可以提取JSON片段(例如,示例中的设备数组)。 如果您需要内部数组(例如设备)的某些信息,您可以使用第二个OPENJSON打开该JSON数组并从内部表中获取信息,例如:

SELECT *
FROM OPENROWSET (BULK 'C:\temp\temp.json', SINGLE_CLOB) as j
CROSS APPLY OPENJSON(BulkColumn, '$.results')
WITH (report_number nvarchar(400), date_received nvarchar(400),device nvarchar(max) AS JSON)
     CROSS APPLY OPENJSON(device) WITH (lot_number int)