Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

azure - Query JSON nested objects using U-SQL

I am trying to get Country and Category from below. I am able to get country but not Category.

Example input:

[{
    "context": {
        "location": {
            "clientip": "0.0.0.0",
            "continent": "Asia",
            "country": "Singapore"
        },
        "custom": {
            "dimensions": [{
                "Category": "Noah Version"
            }]
        }
    }
}]

My Query:

@json =
EXTRACT
    [location] string,
    [device] string,
    [custom.dimensions] string
FROM
    @InputFile
USING new JsonExtractor("context");

@CreateJSONTuple = 
SELECT 
JsonFunctions.JsonTuple([location]) AS LocationData,
JsonFunctions.JsonTuple([device]) AS DeviceData,
JsonFunctions.JsonTuple([custom.dimensions]) AS DimensionData
FROM @json;

@Dataset =
SELECT
LocationData["country"] ?? "NA" AS Country
,LocationData["province"] ?? "NA" AS Province
,LocationData["city"] ?? "N/A" AS City
,DeviceData["osVersion"] ?? "N/A" AS OsVersion
,DeviceData["roleName"] ?? "N/A" AS RoleName
,DimensionData["Category"] ?? "N/A" AS Category
FROM @CreateJSONTuple;


OUTPUT @Dataset
TO @transBlobOutputFile
USING Outputters.Csv(outputHeader:true,quoting:true);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to take in account that the array consists of complex types as well. Also, you cannot just use a notation like EXTRACT [custom.dimensions] string, you have to add itermediate steps for that as well like this:

@json  =
    EXTRACT location String,
            device String,
            custom String
    FROM @InputFile
    USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor("context");

@CreateJSONTuple =
    SELECT JsonFunctions.JsonTuple(location) AS LocationData,
           JsonFunctions.JsonTuple(device) AS DeviceData,
           JsonFunctions.JsonTuple(custom, "dimensions[0]") AS DimensionData
    FROM @json;

@result =
    SELECT LocationData["country"] ?? "N/A" AS Country,
           LocationData["province"] ?? "N/A" AS Province,
           LocationData["city"] ?? "N/A" AS City,
           DeviceData["osVersion"] ?? "N/A" AS OsVersion,
           DeviceData["roleName"] ?? "N/A" AS RoleName,
           DimensionData["dimensions[0]"] AS CategoryDimension
    FROM @CreateJSONTuple;

@result =
    SELECT Country,
           Province,
           City,
           OsVersion,
           RoleName,
           JsonFunctions.JsonTuple(CategoryDimension) AS CategoryDimension
    FROM @result;

@result =
    SELECT Country,
           Province,
           City,
           OsVersion,
           RoleName,
           CategoryDimension["Category"] AS Category
    FROM @result;

OUTPUT @result
TO @output
USING Outputters.Csv(outputHeader : true, quoting : true);

This outputs:

"Country" "Province" "City" "OsVersion" "RoleName" "Category"
"Singapore" "N/A" "N/A" "Microsoft Windows NT 6.1.7601 Service Pack 1" "Connexx" "Noah Version"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...