0

I want to filter Name,Country,Province,City,OsVersion,RoleName,Category from below json.

[{"event":
    [{"name":"4.8","count":1}],
    "internal":{"data":{"id":"da3d8290-41f4-11e8-a8f8-e347c81391d3","documentVersion":"1.61"}},
"context":{
    "data":{"eventTime":"2018-04-17T04:08:27.392Z","isSynthetic":false,"samplingRate":100.0},
    "cloud":{},
    "device":{"type":"PC","osVersion":"Microsoft Windows NT 6.1.7601 Service Pack 1","roleName":"Connexx","roleInstance":"Connexx","screenResolution":{}},
    "session":{"isFirst":false},
    "operation":{},
    "location":{"clientip":"0.0.0.0","continent":"Asia","country":"Singapore"},
    "custom":{"dimensions":[{"Category":"Noah Version"},{"ai.device.language":"en-US"},{"RAMSize":"7.88622283935547"},{".NetVersion":"4.6.2"},{"64BitProcess":"False"},{"64BitOS":"True"},{"ProcessorCount":"4"}]}
          }
    }]

I am using below code but not getting Name.

// A. CREATE ASSEMBLY: Register assemblies (if they do not already exist).
CREATE ASSEMBLY IF NOT EXISTS [Newtonsoft.Json] FROM @"/Assemblies/Newtonsoft.Json.dll";
CREATE ASSEMBLY IF NOT EXISTS [Microsoft.Analytics.Samples.Formats] FROM @"/Assemblies/Microsoft.Analytics.Samples.Formats.dll";

// B. REFERENCE ASSEMBLY: Load assemblies for compile time and execution.
REFERENCE ASSEMBLY [Newtonsoft.Json];
REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];

// C. USING: Specify namespace to shorten function names (e.g. Microsoft.Analytics.Samples.Formats.Json.JsonExtractor)
USING Microsoft.Analytics.Samples.Formats.Json;

DECLARE @InputFile string = @"/TransformedBlobData/Source/{*}";
DECLARE @output string = @"/TransformedBlobData/OutputVatan/transBlobOutput13.csv";


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

@CreateJSONTuple =
    SELECT JsonFunctions.JsonTuple([event], "event[0]") AS EventData,
           JsonFunctions.JsonTuple(location) AS LocationData,
           JsonFunctions.JsonTuple(device) AS DeviceData,
           JsonFunctions.JsonTuple(custom, "dimensions[0]") AS DimensionData0,
           JsonFunctions.JsonTuple(custom, "dimensions[1]") AS DimensionData1,
           JsonFunctions.JsonTuple(custom, "dimensions[2]") AS DimensionData2,
           JsonFunctions.JsonTuple(custom, "dimensions[100]") AS DimensionData100 //Just to see what happen if we go out of range, and it dont give exception
    FROM @json;

@result =
    SELECT EventData  AS NameCount,
            //EventData["[name]"] ?? "N/A" AS NameCount,
           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,
           DimensionData0["dimensions[0]"] AS CategoryDimension0,
           DimensionData1["dimensions[1]"] AS CategoryDimension1,
           DimensionData2["dimensions[2]"] AS CategoryDimension2,
           DimensionData2["dimensions[100]"] AS CategoryDimension100
    FROM @CreateJSONTuple;

@result =
    SELECT NameCount["name"] ?? "N/A" AS Name, 
           Country,
           Province,
           City,
           OsVersion,
           RoleName,
           JsonFunctions.JsonTuple(CategoryDimension0) AS CategoryDimension0,
           JsonFunctions.JsonTuple(CategoryDimension1) AS CategoryDimension1,
           JsonFunctions.JsonTuple(CategoryDimension2) AS CategoryDimension2,
           JsonFunctions.JsonTuple(CategoryDimension100) AS CategoryDimension100
    FROM @result;

@result =
    SELECT Name,
           Country,
           Province,
           City,
           OsVersion,
           RoleName,
           CategoryDimension0["Category"] ?? CategoryDimension1["Category"] ?? CategoryDimension2["Category"] ?? CategoryDimension100["Category"] ?? "N/A" AS Category
    FROM @result
         WHERE Country != "N/A"
         ;

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

I am not getting event.name and it's coming as N/A. Please help This outputs: "N/A" "Singapore" "N/A" "N/A" "Microsoft Windows NT 6.1.7601 Service Pack 1" "Connexx" "Noah Version"

Vatan Soni
  • 540
  • 2
  • 11
  • 23
  • 1
    Hi, this is the third time you have asked this question. What is wrong with the other answers that have been given? for example [this one](https://stackoverflow.com/questions/53064132/query-json-nested-objects-using-u-sql). You would be better off working with that answer and bring it to a conclusion. – wBob Nov 08 '18 at 22:40
  • Hello, some requirements got changed now. It's not same question and as it's a complex json so query will change. – Vatan Soni Nov 09 '18 at 04:16
  • Hi @wBob. If no one answer then will it be forwarded to next level or what will happen??? – Vatan Soni Nov 10 '18 at 22:01

0 Answers0