1

I'm currently developing a Node/Express/MongoDB Restful Service. Now, since MongoDB doesn't have "columns", it easily happens that a response for the same endpoint can contain a specific property or not. e.g.

# GET /users/1    
{"name": "Alexander", "nickname": "Alex"}

# GET /users/2
{"name": "Simon"}

While this doesn't make any difference to handle with weakly typed languages like JavaScript, one of my coworkers who's implementing a client in C#, struggles to parse the JSON string when having missing properties.

IMO, the current approach is better from the API perspective, since it causes better performance, less code and even traffic on the serverside. Otherwise I would need to normalize Objects before sending or even run migrations every time a property gets added. Also it doesn't send "virtual" data which doesn't even exist on the resource.

But on the other hand, I also want to build a solid Service from the clients perspective and "normalizing" on the client side is at least as bad as on the server side.

Theres also another use case, which works well in JS but will cause problems in C# and refers to the same problem:

# GET /users/1/holidays
{
    "2018-12-25": { "title": "Christmas" }, 
    "2019-01-01": { "title": "New Year" }
}

I took this approach to automatically prevent multiple entries for the same days. But I could understand if this is really considered bad practice.

Update

As commented by @jfriend00, the second example is not that handy. So I won't use it anymore.

  • How are they (the c# dev) parsing the json? I hope they are using the Newtonsoft package (or one of the other mature parsers) and NOT trying to do it manually. – Neil Mar 04 '18 at 16:17
  • I'm not sure what the problem would be when deserializing in C#. Using a library like [Newtonsoft](https://www.newtonsoft.com/json) would just deserialize values to null properties if the `Nickname` is not populated. – ColinM Mar 04 '18 at 16:17
  • It seems that as long as you establish what an appropriate default value in the target programming language is for each possible missing property that any parser should be able to deal with that just fine. You probably should also document exactly what the required and optional properties are so the parsing code knows what to plan for and what to consider incomplete data. – jfriend00 Mar 04 '18 at 16:20
  • 1
    Your second structure will be harder to deal with in some other programming languages. They will likely have to turn it into an array of objects where the date is one property on the object in the array. In fact, to consume it in Javascript, you'll also have to at least use `Object.keys()` on it to get an array of keys so it would also be easier to use in Javascript if it was just an array of objects. I'm not really a fan of making the data harder to use just to express that there should be no date duplicates. It seems there are other ways to express that. – jfriend00 Mar 04 '18 at 16:24
  • Thanks for your feedback guys! Well, tbh, the C# dev is fairly new. Especially when it comes to networking. But if I get you right, there's practically nothing wrong with my JSON response and it's basically the C# devs fault? At least in the first example. –  Mar 04 '18 at 16:28
  • 1
    [For your C# dev](https://stackoverflow.com/questions/29611445/default-value-for-missing-properties-with-json-net) – James Mar 04 '18 at 17:05

0 Answers0