WorkHardWork WorkHardWork - 1 year ago 292
JSON Question

USql Call data in multidimensional JSON array

I have this JSON file in a data lake that looks like this:

"text":"Description of post"},
"abstracttext":"More Text and links",

in order to call the data into my application, I have to turn the JSON into a string using this code:

DECLARE @input string = @"/MSEStream/{*}.json";

REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];

@allposts =
jsonString string
FROM @input
USING Extractors.Text(delimiter:'\b', quoting:true);

@extractedrows = SELECT Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(jsonString) AS er FROM @allposts;

@result =
SELECT er["id"] AS postID,
er["contenttype"] AS contentType,
er["posttype"] AS postType,
er["uri"] AS uri,
er["title"] AS Title,
er["acquisitiondate"] AS acquisitionDate,
er["modificationdate"] AS modificationDate,
er["publicationdate"] AS publicationDate,
er["profile"] AS profile
FROM @extractedrows;

OUTPUT @result
TO "/ProcessedQueries/all_posts.csv"
USING Outputters.Csv();

This output the JSON into a .csv file that is readable and when I download the file all data is displayed properly. My problem is when I need to get the data inside profile. Because the JSON is now a string I can't seem to extract any of that data and put it into a variable to use. Is there any way to do this? or do I need to look into other options for reading the data?

Answer Source

You can use JsonTuple on the profile string to further extract the specific properties you want. An example of U-SQL code to process nested Json is provided in this link -

You can use JsonTuple on the profile column to further extract specific nodes

E.g. use JsonTuple to get all the child nodes of the profile node and extract specific values like how you did in your code.

@childnodesofprofile = 
   Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(profile) AS childnodes_map
FROM @result;

@values =
   childnodes_map["name"] AS name,
   childnodes_map["id"] AS id
FROM @result;

Alternatively, if you are interested in specific values, you can also pass paramters to the JsonTuple function to get the specific nodes you want. The code below gets the locality node from the recursively nested nodes (as described by the "$..value" construct.

@locality = 
SELECT Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(profile, "$..locality").Values AS locality
FROM @result;

Other supported constructs by JsonTuple

    JsonTuple(json, "id", "name")              // field names          
    JsonTuple(json, "$")           // nested fields        
    JsonTuple(json, "$..address")              // recursive children   
    JsonTuple(json, "$[?( > 1)].id")       // path expression      
    JsonTuple(json)                            // all children

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download