We were discussing how to design a API response, for simplicity, think of having to give information of all the different types of facilities available in a city:
{
"city": {
"cityName": "Gotham",
"population": "8620000",
"facilities": {
"airport": {
"name": "Batman International Airport",
"numRunways": 7,
"dailyCommuters": 340000
},
"railwayStation": {
"name": "Gotham Downtown Rail Station",
"platforms": 12,
"dailyCommuters": 40000
},
"hospital": {
"name": "Joker Memorial Nursing Home",
"beds": 300
},
"port": null,
"market": null
}
}
}
- For now, we know that there are 5 facilities available generally in cities that our clients are interested in. In future, we might add more facility types to the list, though.
- In the example response, Gotham city has an Airport, Railway Station and a Hospital, but no Port or a Market.
- Each facility is very different from each other, and would have different types of fields in it. We need that flexibility.
Somebody suggested adding an extra buildings
list, to indicate which buildings are present:
{
"city": {
"cityName": "Gotham",
"population": "8620000",
"buildings": {
"buildings": ["AIRPORT", "RAILWAY_STATION", "HOSPITAL"],
"airport": {
"name": "Batman International Airport",
"numRunways": 7,
"dailyCommuters": 340000
},
"railwayStation": {
"name": "Gotham Downtown Rail Station",
"platforms": 12,
"dailyCommuters": 40000
},
"hospital": {
"name": "Joker Memorial Nursing Home",
"beds": 300
},
"port": null,
"market": null
}
}
}
Generalising, if there are lots of fields in an object which might not be set, is it a good practice to specify them explicitly in the response from the UI? What are the pros and cons? What's the best way to do this?