1

What are the best practices for parsing a complex JSON object, in this case being posted to a controller action?

Creating a model class up front is brittle because the sender will change the structure of the JSON over time.

Controller

        // POST api/values
        public void Post([FromBody] AdminNotesModel value)
        {

           // do something with JSON...
        }

Payload example

    {
"type": "notification_event",
"app_id": "cl00zu9g",
"data": {
"type": "notification_event_data",
"item": {
"type": "conversation",
"id": "7594189485",
"created_at": 1483576126,
"updated_at": 1483650387,
"user": {
"type": "user",
"id": "586d933eb76086661e49c991",
"user_id": "83c3224b-45b2-4f7d-a978-40c836564658",
"name": "Byrne MichaelaS",
"email": "michaela.byrne@yellowjackets.bhsu.edu"
},
"assignee": {
"type": "admin",
"id": "848395",
"name": "Linda",
"email": "linda@aol.com"
},
"conversation_message": {
"type": "conversation_message",
"id": "69270153",
"subject": "<p>Summer internship possibilities </p>",
"body": "<p>Hello,</p><p>My name is Joe Foo. </p><p>Thanks for your time and please let me know if this might be a possibility.</p>",
"author": {
"type": "user",
"id": "586d933eb76086661e49c991"
},
"attachments": []
},
"conversation_parts": {
"type": "conversation_part.list",
"conversation_parts": [
{
"type": "conversation_part",
"id": "416288171",
"part_type": "note",
"body": "<p>Sent to Vicki</p>",
"created_at": 1483650387,
"updated_at": 1483650387,
"notified_at": 0,
"assigned_to": null,
"author": {
"type": "admin",
"id": "848395",
"name": "Linda"
},
"attachments": [],
"external_id": null
}
],
"total_count": 1
},
"open": true,
"read": true,
"metadata": {},
"tags": {
"type": "tag.list",
"tags": []
},
"links": {
"conversation_web": "https://app.intercom.io/a/apps/"
}
}
},
"links": {},
"id": "notif_d2e41790-d38a-11e6-b063-f9334405d60a",
"topic": "conversation.admin.noted",
"delivery_status": "retry",
"delivery_attempts": 2,
"delivered_at": 0,
"first_sent_at": 1483650448,
"created_at": 1483650387,
"self": null
}
Slinky
  • 5,662
  • 14
  • 76
  • 130
  • You can accept an object as Parameter: `public void Post([FromBody] object obj){ \\do what ever you want with the obj }` – DomeTune Jan 06 '17 at 13:32

2 Answers2

1
  1. The best solution is to agree on some predefined structure of data, because it will allow much easier error detection and will simplify further handling.
  2. If you can't do it, then I'd recommend to at least agree on some basic required fields and handle everything else dynamically. To achieve it, you will have to replace your public void Post([FromBody] AdminNotesModel value) with public void Post([FromBody] String value) and then apply deserialization manually like it is described in Deserialize json with known and unknown fields. I'd also recommend to consider some possible versioning/typing strategy for dynamic data. Like in version 1 that additional data is some AdditionalDataVer1 class, while in version 2 it is AdditionalDataVer2.
  3. If you still consider absolutely dynamic schema for your data, then you will just have to deserialize received string value to some dynamic or less structured form.
Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • Thanks, for the suggestions and explanations; very helpful. I am going to stick to my guns with a model class – Slinky Jan 06 '17 at 20:24
0

I personally use RestSharp to work with API and HTTP methods. Please, take a look at: http://restsharp.org/. You may find it very useful.