Jan Fellien
Jan Fellien

Reputation: 43

How to create complex types with Azure Stream Analytics queries

I try to transform flat JSON data from an Event Hub into a DocumentDB. The target structure should look like:

{
   "id" : 1
   "field_1" : "value_1",
   "details" : {
      "detail_field_1":"abc",
      "detail_field_2":"def"
   }
}

Created from source:

{
   "id":1,
   "field_1" : "value_1",
   "detail_field_1":"abc",
   "detail_field_2":"def"
}

I checked the documentation of Azure Stream Analytics but there ist no clear description how to create a proper Query.

Who one can help me?

Upvotes: 0

Views: 878

Answers (2)

Xavier John
Xavier John

Reputation: 9447

Using JavaScript UDF feature, you can return complex JSON. Example write function.

function main(obj) {
//get details object from input payload
var details_obj = {};

details_obj.detail_field_1 = obj.detail_field_1;
details_obj.detail_field_2 = obj.detail_field_2;

 return details_obj;
}

You should not use JSON.stringify since it will make it a string instead of JSON object.

Use it like.

SELECT id,  field_1, UDF.getDetails(input) As details
INTO output
FROM input

Upvotes: 0

MinHe-MSFT
MinHe-MSFT

Reputation: 279

You can leverage the new JavaScript UDF feature to write nested JSON objects to output.

Register a user-defined function, "UDF.getDetails()" as below:

function main(obj) {
  //get details object from input payload
  var details_obj = {};

  details_obj.detail_field_1 = obj.detail_field_1;
  details_obj.detail_field_2 = obj.detail_field_2;

  return JSON.stringify(details_obj);
}

Then call the UDF in your query to get a string of the nested JSON object.

SELECT
  id,
  field_1,
  UDF.getDetails(input) As details
INTO output
FROM input

Upvotes: 3

Related Questions