2017-10-09 13 views
0

私はこれを前後に行ってきましたが、困惑しています。私は、複数のオブジェクトにまたがって複数のJSON行があるファイルを持っています。Json行(.jsonlファイル)とSQL Server 2016

私は2行を下に置きました。

{ 
    "mental_health_act_legal_status": 
    [ 
     {"legal_status_classification": "16", "start_time": "16:32", "expiry_date": "20171014", "expiry_time": "20:28", "start_date": "20170912"}, 
     {"legal_status_classification": "07", "start_time": "01:31", "expiry_date": "20170922", "expiry_time": "17:53", "start_date": "20170820"}, 
     {"legal_status_classification": "36", "start_time": "00:25", "expiry_date": "20170909", "expiry_time": "18:08", "start_date": "20170801"}, 
     {"legal_status_classification": "18", "start_time": "18:26", "expiry_date": "20170801", "expiry_time": "12:40", "start_date": "20170724"}, 
     {"legal_status_classification": "18", "start_time": "10:26", "expiry_date": "20170801", "expiry_time": "03:07", "start_date": "20170623"}, 
     {"legal_status_classification": "18", "start_time": "04:11", "expiry_date": "20170621", "expiry_time": "12:51", "start_date": "20170601"}, 
     {"legal_status_classification": "17", "start_time": "07:54", "expiry_date": "20170605", "expiry_time": "05:04", "start_date": "20170512"}, 
     {"legal_status_classification": "31", "start_time": "02:41", "expiry_date": "20170520", "expiry_time": "11:14", "start_date": "20170920"}, 
     {"legal_status_classification": "03", "start_time": "23:17", "expiry_date": "20170504", "expiry_time": "06:29", "start_date": "20170925"}, 
     {"legal_status_classification": "05", "start_time": "17:41", "expiry_date": "20170403", "expiry_time": "01:23", "start_date": "20170331"}, 
     {"legal_status_classification": "36", "start_time": "05:05", "expiry_date": "20170322", "expiry_time": "14:30", "start_date": "20170312"}, 
     {"legal_status_classification": "03", "start_time": "03:45", "expiry_date": "20170310", "expiry_time": "11:56", "start_date": "20170213"}, 
     {"legal_status_classification": "01", "start_time": "14:03", "expiry_date": "20170201", "expiry_time": "06:02", "start_date": "20170118"}, 
     {"legal_status_classification": "09", "start_time": "07:07", "expiry_date": "20170121", "expiry_time": "22:15", "start_date": "20170111"} 
    ], 
    "system": {"record_id": "1484925971009999954", "provider_record_id": "RTD   0167983  0000046", "sus_version": "20170927072844117", "spell_id": "1484925971009999954"} 
} 
{ 
    "mental_health_act_legal_status": 
    [ 
     {"legal_status_classification": "15", "start_time": "18:51", "expiry_date": "20170919", "expiry_time": "23:04", "start_date": "20170904"}, 
     {"legal_status_classification": "19", "start_time": "06:11", "expiry_date": "20170908", "expiry_time": "09:17", "start_date": "20170818"}, 
     {"legal_status_classification": "15", "start_time": "20:36", "expiry_date": "20170818", "expiry_time": "07:44", "start_date": "20170723"}, 
     {"legal_status_classification": "01", "start_time": "16:12", "expiry_date": "20170809", "expiry_time": "21:25", "start_date": "20170718"}, 
     {"legal_status_classification": "09", "start_time": "06:06", "expiry_date": "20170707", "expiry_time": "18:10", "start_date": "20170615"}, 
     {"legal_status_classification": "07", "start_time": "11:53", "expiry_date": "20170625", "expiry_time": "14:09", "start_date": "20170527"}, 
     {"legal_status_classification": "19", "start_time": "07:13", "expiry_date": "20170529", "expiry_time": "00:39", "start_date": "20170503"}, 
     {"legal_status_classification": "35", "start_time": "16:01", "expiry_date": "20170521", "expiry_time": "07:18", "start_date": "20170912"}, 
     {"legal_status_classification": "35", "start_time": "02:45", "expiry_date": "20170430", "expiry_time": "06:47", "start_date": "20170902"}, 
     {"legal_status_classification": "03", "start_time": "19:51", "expiry_date": "20170410", "expiry_time": "18:46", "start_date": "20170319"}, 
     {"legal_status_classification": "13", "start_time": "20:58", "expiry_date": "20170310", "expiry_time": "10:52", "start_date": "20170220"}, 
     {"legal_status_classification": "02", "start_time": "13:11", "expiry_date": "20170217", "expiry_time": "00:42", "start_date": "20170127"}, 
     {"legal_status_classification": "15", "start_time": "18:23", "expiry_date": "20170209", "expiry_time": "20:03", "start_date": "20170120"}, 
     {"legal_status_classification": "07", "start_time": "00:59", "expiry_date": "20170119", "expiry_time": "12:33", "start_date": "20161231"} 
    ], 
    "system": {"record_id": "1484925971009999916", "provider_record_id": "RTD   0167983  0000084", "sus_version": "20170927072844117", "spell_id": "1484925971009999916"} 
} 

私はopenjson構文を使用している場合、私はそれは私がテーブルの1だけ、ファイル内の1行がロードされているにしたい部分をロードするために取得することができます。私はファイル全体を調べる必要があります。

次に、hereの投稿から.fmtファイルを見ましたが、私がこれまで使ってきたことから、JSON行はすべて同じテーブルに移動しなければならないということです。行内の複数の表に複数のオブジェクトを移動させることはできません。また、それらを分割する場所もわかりません。

システムやメンタルヘルス行為の法的地位をカバーするだけの二つのテーブルのために、SQLにこれを取得するための私のコードは次のとおりです。

DECLARE @JSON NVARCHAR(MAX) 

SET @JSON = (SELECT J.* 
      FROM OPENROWSET 
      (BULK 'C:\Users\DACR9\Desktop\ECDS_DATA\emergency_care_report.jsonl', SINGLE_CLOB) 
      AS j) 

SELECT * FROM OPENJSON (@JSON, '$.system') 
    WITH ([SUS_VERSION] VARCHAR (255) '$.sus_verion', 
     [SPELL_ID] VARCHAR (255) '$.spell_id', 
     [RECORD_ID] VARCHAR (255)'$.record_id', 
     [PROVIDER_RECORD_ID] VARCHAR (255) '$.provider_record_id' 
    ) 

SELECT [record_id], [legal_status_classification], [start_date], [start_time], [expiry_date], [expiry_time] 
FROM OPENROWSET (BULK 'C:\Users\DACR9\Desktop\ECDS_DATA\emergency_care_report.jsonl', SINGLE_CLOB) AS j 
CROSS APPLY OPENJSON (BulkColumn, '$.mental_health_act_legal_status') 
WITH (
    [legal_status_classification] VARCHAR (255), 
    [start_time] VARCHAR (255), 
    [expiry_date] VARCHAR (255), 
    [expiry_time] VARCHAR (255), 
    [start_date] VARCHAR (255) 
    ) 
CROSS APPLY OPENJSON (BulkColumn, '$.system') 
WITH (
    [record_id] VARCHAR (255) 
    ) 

がどのルートを見てアドバイスを提供したり、助けたりすることができいずれかでください。 。

おかげ

+0

..だからあなたはFROM最初の 'SELECT *を言っていますOPENJSON(@JSON、 '$ .system') 'は最初の行だけを返しますか?まず、変数 '@ JSON'に' 'system": '行が複数含まれていることを確認してください。 –

+0

こんにちは。複数の「システム」行を含みます。システムは、システム生成のrecord_idを含んでいるため、そこになければならない必須のセクションです。すべては技術的にオプションですが、例の場合はMHAで追加しました。 record_idは、MHA行を特定の個々のレコードに識別するもので、識別子がなく、record_idがシステムブロック内のPKです。私は2行をコピーし、それをMHAとシステムにトリムして見やすくしました。 – user1663003

+0

基本的に、 ''システム ''が配列のレコードであることを示すものはありません。 OPENJSONを読んでいると、JSON配列から複数のレコードしか取得できないことを示しています。 JSON配列は '[{a thing}、{elsething}、{another one}} 'の形式です。明らかに、このファイルの形式を変更する選択肢はありません。私は以下の私の答えを編集しました。それはそれの終わりではないかもしれませんが、サンプルはこれが必要なフォーマットであることを示すように見えます。 –

答えて

0

私は、これは「整形JSON」ではないと思います - 最初の行であれば末尾にカンマがあってはなりませんか?大括弧で囲ってはいけませんか?何かがこのJSONを生成しましたか?あなたはここから持ち上げられたサンプルを見てみる場合は

https://docs.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sqlあなたは、中括弧に含まれる複数の注文のレコードがカンマで区切らとあなたがにオフデータをプッシュすることができます

DECLARE @json NVARCHAR(MAX) = N'[ 
    { 
    "Order": { 
     "Number":"SO43659", 
     "Date":"2011-05-31T00:00:00" 
    }, 
    "AccountNumber":"AW29825", 
    "Item": { 
     "Price":2024.9940, 
     "Quantity":1 
    } 
    }, 
    { 
    "Order": { 
     "Number":"SO43661", 
     "Date":"2011-06-01T00:00:00" 
    }, 
    "AccountNumber":"AW73565", 
    "Item": { 
     "Price":2024.9940, 
     "Quantity":3 
    } 
    } 
]' 

SELECT * 
FROM OPENJSON (@json) 
WITH ( 
       Number varchar(200) '$.Order.Number', 
       Date  datetime  '$.Order.Date', 
       Customer varchar(200) '$.AccountNumber', 
       Quantity int   '$.Item.Quantity', 
       [Order] nvarchar(MAX) AS JSON 
) 

角括弧

に囲まれて見ることができます異なるテーブル。 OpenJSONの制限ではなく、ステージングテーブルなどを使用して分割する方法を検討するだけです。私はこれが本当に答えではないことを認識していますが、興味があれば詳細を追加します。

私はこの構文を使用して複数の行を取得することができますが、私は、配列のために角括弧を追加しただけであるため、と要素の間にカンマを入れて:

DECLARE @JSON VARCHAR(MAX); 

SET @JSON = ' 
[{ 
    "mental_health_act_legal_status": 
    [ 
     {"legal_status_classification": "16", "start_time": "16:32", "expiry_date": "20171014", "expiry_time": "20:28", "start_date": "20170912"}, 
     {"legal_status_classification": "07", "start_time": "01:31", "expiry_date": "20170922", "expiry_time": "17:53", "start_date": "20170820"}, 
     {"legal_status_classification": "36", "start_time": "00:25", "expiry_date": "20170909", "expiry_time": "18:08", "start_date": "20170801"}, 
     {"legal_status_classification": "18", "start_time": "18:26", "expiry_date": "20170801", "expiry_time": "12:40", "start_date": "20170724"}, 
     {"legal_status_classification": "18", "start_time": "10:26", "expiry_date": "20170801", "expiry_time": "03:07", "start_date": "20170623"}, 
     {"legal_status_classification": "18", "start_time": "04:11", "expiry_date": "20170621", "expiry_time": "12:51", "start_date": "20170601"}, 
     {"legal_status_classification": "17", "start_time": "07:54", "expiry_date": "20170605", "expiry_time": "05:04", "start_date": "20170512"}, 
     {"legal_status_classification": "31", "start_time": "02:41", "expiry_date": "20170520", "expiry_time": "11:14", "start_date": "20170920"}, 
     {"legal_status_classification": "03", "start_time": "23:17", "expiry_date": "20170504", "expiry_time": "06:29", "start_date": "20170925"}, 
     {"legal_status_classification": "05", "start_time": "17:41", "expiry_date": "20170403", "expiry_time": "01:23", "start_date": "20170331"}, 
     {"legal_status_classification": "36", "start_time": "05:05", "expiry_date": "20170322", "expiry_time": "14:30", "start_date": "20170312"}, 
     {"legal_status_classification": "03", "start_time": "03:45", "expiry_date": "20170310", "expiry_time": "11:56", "start_date": "20170213"}, 
     {"legal_status_classification": "01", "start_time": "14:03", "expiry_date": "20170201", "expiry_time": "06:02", "start_date": "20170118"}, 
     {"legal_status_classification": "09", "start_time": "07:07", "expiry_date": "20170121", "expiry_time": "22:15", "start_date": "20170111"} 
    ], 
    "system": {"record_id": "1484925971009999954", "provider_record_id": "RTD   0167983  0000046", "sus_version": "20170927072844117", "spell_id": "1484925971009999954"} 
},{ 
    "mental_health_act_legal_status": 
    [ 
     {"legal_status_classification": "15", "start_time": "18:51", "expiry_date": "20170919", "expiry_time": "23:04", "start_date": "20170904"}, 
     {"legal_status_classification": "19", "start_time": "06:11", "expiry_date": "20170908", "expiry_time": "09:17", "start_date": "20170818"}, 
     {"legal_status_classification": "15", "start_time": "20:36", "expiry_date": "20170818", "expiry_time": "07:44", "start_date": "20170723"}, 
     {"legal_status_classification": "01", "start_time": "16:12", "expiry_date": "20170809", "expiry_time": "21:25", "start_date": "20170718"}, 
     {"legal_status_classification": "09", "start_time": "06:06", "expiry_date": "20170707", "expiry_time": "18:10", "start_date": "20170615"}, 
     {"legal_status_classification": "07", "start_time": "11:53", "expiry_date": "20170625", "expiry_time": "14:09", "start_date": "20170527"}, 
     {"legal_status_classification": "19", "start_time": "07:13", "expiry_date": "20170529", "expiry_time": "00:39", "start_date": "20170503"}, 
     {"legal_status_classification": "35", "start_time": "16:01", "expiry_date": "20170521", "expiry_time": "07:18", "start_date": "20170912"}, 
     {"legal_status_classification": "35", "start_time": "02:45", "expiry_date": "20170430", "expiry_time": "06:47", "start_date": "20170902"}, 
     {"legal_status_classification": "03", "start_time": "19:51", "expiry_date": "20170410", "expiry_time": "18:46", "start_date": "20170319"}, 
     {"legal_status_classification": "13", "start_time": "20:58", "expiry_date": "20170310", "expiry_time": "10:52", "start_date": "20170220"}, 
     {"legal_status_classification": "02", "start_time": "13:11", "expiry_date": "20170217", "expiry_time": "00:42", "start_date": "20170127"}, 
     {"legal_status_classification": "15", "start_time": "18:23", "expiry_date": "20170209", "expiry_time": "20:03", "start_date": "20170120"}, 
     {"legal_status_classification": "07", "start_time": "00:59", "expiry_date": "20170119", "expiry_time": "12:33", "start_date": "20161231"} 
    ], 
    "system": {"record_id": "1484925971009999916", "provider_record_id": "RTD   0167983  0000084", "sus_version": "20170927072844117", "spell_id": "1484925971009999916"} 
}]'; 


SELECT * FROM OPENJSON (@JSON) 
    WITH ([SUS_VERSION] VARCHAR (255) '$.system.sus_verion', 
     [SPELL_ID] VARCHAR (255) '$.system.spell_id', 
     [RECORD_ID] VARCHAR (255)'$.system.record_id', 
     [PROVIDER_RECORD_ID] VARCHAR (255) '$.system.provider_record_id' 
    ); 
+0

こんにちはニック。ご意見ありがとうございます。実際の回線の1つを使用するように投稿を編集しましたが、投稿していた昨夜、電源が切れていたラップトップにアクセスできませんでした。私はまた、特定の部分を取り出すためのスクリプトを追加しましたが、私はすべてを行うために1つを書いています。私の問題は、ファイルemergency_careに行くときです。jsonlはSQLが見つかった最初の行でのみ実行されます。私はファイル内のすべての行を通過させようとしています。これは、ロードルーチンを開発するために形成されたすべてのテストデータです。ありがとう。 – user1663003

関連する問題