Ankit Kumar
Ankit Kumar

Reputation: 514

How to filter the response from Service bus Topic messages

I have the following response coming from my Function app with Service Bus Topic Trigger and here I would like to filter out the records ID having DOC TYPE AS "REC" and ComputerModel as "Yuli". So for below response which I am getting from my service bus trigger, I should get the value ID : "MARAS_1S4810L8010016000075_20201015T130058Z" which I am going to further query in the code. I would like some help on completing this piece of code for me.

Response mySbMsg I get in Console:

[{"Id":"MARAS_1S4810L8010016000075_20201015T130058Z","DocType":"REC","Tags":[],"Properties":{"Access":"COMMON","BIOSSerialNumber":"0075","CalibHash":"aoI8zg","ComputerBuild":"EE-FVT","ComputerModel":"Yuii","Customer":"MAARS","Location":"Mumb","LogPresenceDuration":"120","Model":"YTOPI 44880132-aoI8zg","CompanyXYZ":"false","RadarFPS":"4","RadarModule":"YTOPI","RadarMount":"Yuii","RadarRevision":"Rev4","RecorderRelease":"Recorder_Platform_Minimal-Release049_20200924T143831","RecordingType":"UX","ResistorValueOhm":"75","ScreenTiltAngle":"120","UniqueSystemID":"1S4810L8010016000075","UserChairX":"0","UserChairZ":"0","UserSittingMode":"lean right side","UserWalkInRepetitions":"1","uDriverVersion":"v2_0_6"},"Categories":[],"Trigger":true,"Received":"2020-10-15T14:58:25.3357416Z"}, {"Id":"MAARS_Yuii_UX|MAARS_2.1.1.1_1S4810L8010016000075_20201015T130732Z","DocType":"ASS","Tags":[],"Properties":{"Access":"COMMON","BIOSSerialNumber":"16000075","CalibHash":"aoI8zg","ComputerBuild":"EE-FVT","ComputerModel":"Yuii","Customer":"MAARS","Location":"Mumb","LogPresenceDuration":"120","Model":"YTOPI 44880132-aoI8zg","CompanyXYZ":"false","RadarFPS":"4","RadarModule":"YTOPI","RadarMount":"Yuii","RadarRevision":"Rev4","RecorderRelease":"Recorder_Platform_Minimal-Release049_20200924T143831","RecordingType":"UX","ResistorValueOhm":"75","ScreenTiltAngle":"120","UniqueSystemID":"1S4810L8010016000075","UserChairX":"0","UserChairZ":"0","UserSittingMode":"lean back","UserWalkInRepetitions":"1","uDriverVersion":"v2_0_6"},"Categories":[],"Trigger":true,"Received":"2020-10-15T14:58:25.3357755Z"}]

Code:

    namespace ServiceBusCopyFunction
{
    public static class GetMetadataFromSB
    {
        
        [FunctionName("GetMetadataFromSB")]
        public static void Run([ServiceBusTrigger("TopicName", "Topic Subscription", Connection = "AzureServiceBusString")] string mySbMsg, ILogger log)
        {
            log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");

        }
    }
}

Upvotes: 0

Views: 77

Answers (1)

Ankit Kumar
Ankit Kumar

Reputation: 514

I figured it out, learnt how to create Model and Deserialize JSON data. here is the code doing the filtering on 'DocType' properties:

 var jsonSerializerSettings = new JsonSerializerSettings();
            jsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
            List<MyItem> lists = JsonConvert.DeserializeObject<List<MyItem>>(mySbMsg, jsonSerializerSettings);
            List<string> filteredResult = (from s in lists
                                          where s.DocType == "REC"
                                 select s.Id).ToList();

        }
        catch(Exception e )
        {
            log.LogError( $"Error while catching messages; {e.Message}\n" );
            
        }

    }

    public class MyItem
    {
        public string Id;
        public string DocType;
        public string Project;
        public string ProjectId;
        public MyProperties Properties;
       
        
    }
    public class MyProperties
    {
       public string Access;
       public string BIOSSerialNumber;
    }

}
}

Upvotes: 1

Related Questions