USQL Query to create a Table from Json Data

前端 未结 1 1510
孤独总比滥情好
孤独总比滥情好 2020-12-22 00:04

I have a json which is like [{}, {}, {}], i.e. there can be multiple rows and each row has a number of property - value pairs, which remain fixed for each row.<

相关标签:
1条回答
  • 2020-12-22 00:28

    Found a solution below that worked in my small test case - I agree that the {"Name: 'XXX', Value: 'YYY'} formatting makes things a little tricky but you can still get this done without a custom extractor - just lots of nested JSON.

    Basically, you extract a SQLArray from the System and Details JSON (in @parse_json), then split those into smaller JSON tuples (in @get_nested), and then finally extract the "Values" in @output. See code below. Let me know if you have questions!

    
    
        CREATE ASSEMBLY [Microsoft.Analytics.Samples.Formats]
        FROM @FormatsAssembly;
    
        CREATE ASSEMBLY [Newtonsoft.Json]
        FROM @JSONAssembly;
    
        REFERENCE ASSEMBLY [Newtonsoft.Json];
        REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];
    
        DECLARE @CommentsPath = "/JSONTest/rawJson.json";
    
    
        @get_json =
            EXTRACT 
                MainId          int,
                System          string,
                Details         string,
                ttl             int
            FROM @CommentsPath
            USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor();
    
    
        @parse_json =
            SELECT MainId, 
                   Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(System).Values AS SystemJson,
                   Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(Details).Values AS DetailsJson,
                   ttl
            FROM @get_json;
    
    
        @get_nested =
            SELECT 
                   MainId,
                   Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(SystemJson[0]) AS SystemName,
                   Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(SystemJson[1]) AS SystemId,
                   Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(DetailsJson[0]) AS DetailsName,
                   Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(DetailsJson[1]) AS DetailsPrevid,
                   ttl
            FROM @parse_json;
    
        @output =
            SELECT MainId,
                   SystemName["Value"] AS SystemEvent,
                   SystemId["Value"] AS SystemId,
                   DetailsName["Value"] AS EventName,
                   DetailsPrevid["Value"] AS PrevId,
                   ttl
            FROM @get_nested;
    
        OUTPUT @output 
            TO @"/JSONTest/test1.csv"
            USING Outputters.Csv(outputHeader : true);
    
    
    0 讨论(0)
提交回复
热议问题