2

I am working with the Telnyx api. One of the api responses returns data that looks like this:

    {
  "data": {
    "event_type": "fax.received",
    "id": "e15c28d4-147e-420b-a638-2a2647315577",
    "occurred_at": "2021-11-19T16:37:02.863682Z",
    "payload": {
      "call_duration_secs": 35,
      "connection_id": "1771912871052051547",
      "direction": "inbound",
      "fax_id": "2a168c93-3db5-424b-a408-b70a3da625bc",
      "from": "+12399999999",
      "media_url": "https://s3.amazonaws.com/faxes-prod/999",
      "page_count": 1,
      "partial_content": false,
      "status": "received",
      "to": "+12399999999",
      "user_id": "dc6e79fa-fe3b-462b-b3a7-5fb7b3111b8a"
    },
    "record_type": "event"
  },
  "meta": {
    "attempt": 1,
    "delivered_to": "https://webhook.site/27ef892c-c371-4976-ae22-22deea57080e"
  }
}

I have read articles on deserializing json to a class object, but I am confused with how to deserialize the payload object within the data. Any help would be appreciated.

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
Greybeard
  • 67
  • 8

1 Answers1

3

Import Newtonsoft.Json from Nuget

Use this:

 Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(Your_Response_From_API); 

Create the following classes:

        public class Payload
        {
            public int call_duration_secs { get; set; }
            public string connection_id { get; set; }
            public string direction { get; set; }
            public string fax_id { get; set; }
            public string from { get; set; }
            public string media_url { get; set; }
            public int page_count { get; set; }
            public bool partial_content { get; set; }
            public string status { get; set; }
            public string to { get; set; }
            public string user_id { get; set; }
        }
    
        public class Data
        {
            public string event_type { get; set; }
            public string id { get; set; }
            public DateTime occurred_at { get; set; }
            public Payload payload { get; set; }
            public string record_type { get; set; }
        }
    
        public class Meta
        {
            public int attempt { get; set; }
            public string delivered_to { get; set; }
        }
    
        public class Root
        {
            public Data data { get; set; }
            public Meta meta { get; set; }
        }

You can of course, rename your "Root" class

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
  • 3
    You can use https://json2csharp.com/ to create the classes – M. Mennan Kara Nov 20 '21 at 00:12
  • 1
    @M.MennanKara yes! Sorry, I forgot include that reference. I used that web. – Leandro Bardelli Nov 20 '21 at 00:17
  • Also, [`JsonProperty`](https://stackoverflow.com/a/15916121/1134705) attribute is a great way to keep your naming conventions and standards. – jnthnjns Nov 20 '21 at 01:40
  • Thanks for the response. I'm getting the error "Unexpected character encountered while parsing value: S. Path '', line 0, position ". I set up the endpoint public IActionResult InboundFax(Object json) and then to deserialize Root myDeserializedClass = JsonConvert.DeserializeObject(json.ToString());. – Greybeard Nov 20 '21 at 02:39
  • @Greybeard could you write here the answer string before the parse? Could be the json is not very well formatted. Let me see it – Leandro Bardelli Nov 20 '21 at 13:41
  • I'll post the actual data in a few minutes. But the examples, which seem to be consistent with the data are at https://developers.telnyx.com/docs/v2/programmable-fax/tutorials/receive-a-fax-via-api – Greybeard Nov 20 '21 at 16:30
  • I just tried posting the data that I posted earlier in this thread using Postman and am getting the same error message. I'm wondering if it is possibly the Object json signature? – Greybeard Nov 21 '21 at 02:17
  • @Greybeard for sure the problem is the data returned, not your code or petition in POSTMAN. The service could be bugged for something. – Leandro Bardelli Nov 21 '21 at 17:55
  • @Greybeard if my answer helps you please don't forgot to mark it as correct. – Leandro Bardelli Nov 21 '21 at 17:56
  • Thanks again for the response. Perhaps I'm being stupid, but I really don't think it is the data. When I take the json data that I posted above and run it through a validator (https://jsonlint.com), the json validates. I then paste it into Postman and it fails. Additionally, I trigger a call from the api to the webhook, which also fails for the same reason. – Greybeard Nov 21 '21 at 23:31
  • @Greybeard I don't think so, maybe is not the data. But I can't understand why it's happen with this information. Is there any way I can test it with postman? If you give me the data? Or is personal? – Leandro Bardelli Nov 22 '21 at 00:18
  • BTW, the data that triggers the error is the same that you posted? – Leandro Bardelli Nov 22 '21 at 00:19
  • yes, the dat I posted triggers the error. Also, vendor api shows demo data at https://developers.telnyx.com/docs/v2/programmable-fax/tutorials/receive-a-fax-via-api. I created a model exactly like the example you showed above. The endpoint has a signature public IActionResult InboundFax(myDeserializedClass json). I can open up the endpoint publicly for you to post to, but it would be identical to what you posted earlier. Are you able to get it to work using the data posted earlier along with your code sample? Thanks again for the help – Greybeard Nov 22 '21 at 02:31
  • Checking and working on your data. Why do you get the action as Object? json is a string (?) – Leandro Bardelli Nov 22 '21 at 12:22
  • I have tried multiple iterations as a string, object, and list, none of which work. – Greybeard Nov 22 '21 at 13:01
  • Mmmmm could you please put the code that you are using? I don't understand why is given error, everything seems to be all right – Leandro Bardelli Nov 22 '21 at 15:07
  • [HttpPost] public IActionResult InboundFax(myDeserializedClass json) { try { Root myDeserializedClass = JsonConvert.DeserializeObject(json.ToString().Trim()); return Content("OK"); } catch(Exception ex) { return Content(ex.ToString()); } } – Greybeard Nov 22 '21 at 15:29
  • Oh, I think you are having another problem here. Is about how do you get the information to "Inbound". Seems to be a problem of mapping from inbound. I suggest you close this question and make another one with all data in how do you get this data from the webhook. Tag me if you want – Leandro Bardelli Nov 22 '21 at 15:38