skip to Main Content

I am not able to parse the below object in C# .NET. I am getting parsing object error.

messageBody (in String format – I am getting this below object in string format)

{
  "Type" : "Nti_1",
  "MessageId" : "c1b7cd5b-3099-532e-9539-91376eea607b",
  "SequenceNumber" : "10000000000000128000",
  "TopicArn" : "arn:aws:sns:us-east-1:xxxxxxx:Project1-SNS.fifo",
  "Message" : "{'Prop1':'202020','Prop2':'Hi-I again reached','Prop3':'Testing String'}",
  "Timestamp" : "2023-02-05T07:35:15.905Z",
  "UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:xxxxxx:PushNotification-SNS.fifo:08d0fac2-ac0f-4ff9-b583-61024a98672d",
  "MessageAttributes" : {
    "EventType" : {"Type":"String.Array","Value":"["SMS","ArialRoot"]"}
  }
}

Below is the classes created to parse above object

public class ParentObject
{
    public string Type { get; set; }
    public string MessageId { get; set; }
    public string SequenceNumber { get; set; }

    public string TopicArn { get; set; }

    public ChildObject Message { get; set; }
    public string Timestamp { get; set; }

    public string UnsubscribeURL { get; set; }

    public string MessageAttributes { get; set; }
}


public class ChildObject
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
}

Below is the code which I am trying to parse the object

ParentObject obj = JsonConvert.DeserializeObject<ParentObject>(messageBody)

I dont know what I am missing to parse the string to specified above object.

4

Answers


  1. MessageAttributes is a string in your .net type and an object in your json.

    Login or Signup to reply.
  2. Try this,

    var parent = JsonConvert.DeserializeObject<ParentObject>(messageBody);
    var child = JsonConvert.DeserializeObject<ChildObject>(parent.Message);
    parent.Message = child;
    
    Login or Signup to reply.
  3. You need to create a C# class that matches the structure of the JSON

    using Newtonsoft.Json;
    
    public class MyClass
    {
        public string Type { get; set; }
        public string MessageId { get; set; }
        public string SequenceNumber { get; set; }
        public string TopicArn { get; set; }
        public string Message { get; set; }
        public string Timestamp { get; set; }
        public string UnsubscribeURL { get; set; }
        public MessageAttributes MessageAttributes { get; set; }
    }
    
    public class MessageAttributes
    {
        public EventType EventType { get; set; }
    }
    
    public class EventType
    {
        public string Type { get; set; }
        public string Value { get; set; }
    }
    

    Deserialize the JSON string

    MyClass obj = JsonConvert.DeserializeObject<MyClass>(json);
    
    Login or Signup to reply.
  4. you json is not valid, you can fix it using replace string

    messageBody = messageBody.Replace(""[","[").Replace("]"","]");
    

    also you Message property is string, if you want to make it json you have to fix it too

    messageBody = messageBody.Replace("'", """).Replace(""{", "{").Replace("}"", "}");
    ParentObject obj = JsonConvert.DeserializeObject<ParentObject>(messageBody);
    

    or you can fix a Message property using this code

    JObject jObj = JObject.Parse(messageBody);
    jObj["Message"] = JObject.Parse((string) jObj["Message"]);
    ParentObject obj = jObj.ToObject<ParentObject>();
    

    also fix the classes

    public class ParentObject
    {
        public string Type { get; set; }
        public string MessageId { get; set; }
        public string SequenceNumber { get; set; }
        public string TopicArn { get; set; }
        public Message Message { get; set; }
        public DateTime Timestamp { get; set; }
        public string UnsubscribeURL { get; set; }
        public MessageAttributes MessageAttributes { get; set; }
    }
    public class EventType
    {
        public string Type { get; set; }
        public List<string> Value { get; set; }
    }
    
    public class MessageAttributes
    {
        public EventType EventType { get; set; }
    }
    
    public class Message
    {
        public string Prop1 { get; set; }
        public string Prop2 { get; set; }
        public string Prop3 { get; set; }
    }
    

    but it is better if you fix a message body in the source to this one

    {
      "Type" : "Nti_1",
      "MessageId" : "c1b7cd5b-3099-532e-9539-91376eea607b",
      "SequenceNumber" : "10000000000000128000",
      "TopicArn" : "arn:aws:sns:us-east-1:xxxxxxx:Project1-SNS.fifo",
      "Message" : {"Prop1":"202020","Prop2":"Hi-I again reached","Prop3":"Testing String"},
      "Timestamp" : "2023-02-05T07:35:15.905Z",
      "UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:xxxxxx:PushNotification-SNS.fifo:08d0fac2-ac0f-4ff9-b583-61024a98672d",
      "MessageAttributes" : {
        "EventType" : {"Type":"String.Array","Value":["SMS","ArialRoot"]}
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search