JSONフラグメントを次のように修正した後:
{
"relation": {
"relationid": "123456",
"name": "relation1",
"addresses": {
"address": [{
"addressid": "1",
"street": "Street 1",
"postcode": "1234 AB",
"city": "City 1"
}, {
"addressid": "2",
"street": "Street 2",
"postcode": "5678 CD",
"city": "City 2"
}]
}
}
}
それをファイルに入れると、次のスクリプトで必要なものが得られます。より高いレベルのアイテムを運ぶために構造を下に移動する必要があることに注意してください。配列に遭遇したら、CROSS APPLY EXPLODE
配列を持つものだけが必要な場合はそれを使用するか、配列が欠落している行が必要な場合はそれを OUTER APPLY EXPLODE します。
DECLARE @input string = @"/temp/stackoverflow.json";
REFERENCE ASSEMBLY [Newtonsoft.Json];
REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];
@json =
EXTRACT relationid int,
name string,
addresses string
FROM @input
USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor("relation");
@relation =
SELECT relationid,
name,
Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(addresses)["address"] AS address_array
FROM @json;
@addresses =
SELECT relationid,
name,
Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(address) AS address
FROM @relation
CROSS APPLY
EXPLODE (Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(address_array).Values) AS A(address);
@result =
SELECT relationid,
name,
address["addressid"]AS addressid,
address["street"]AS street,
address["postcode"]AS postcode,
address["city"]AS city
FROM @addresses;
OUTPUT @result
TO "/users/temp/st_out.csv"
USING Outputters.Csv();