1

In my node.js application, i can send the response to a api call, as a json object or as a json array. Which among these is recommended and why? for my convienience sending the json array is easy, should i follow what is easy for me. I want to send the array of objects!

I tried to google around to get the solution, but i couldn't find.

response object can be like

{
 list:[{},{}]
}

or

[{},{}]
Kishore S
  • 163
  • 3
  • 10
  • What are you sending in the response? This is quite important when asking what type is better – Tea_Lover_418 Aug 08 '19 at 13:08
  • 2
    As said above, it'll completely depend on what you're trying to do with it. Usually people to label the top level object. For example if I call `GET /books` then it'd be quite common to get back `{ "books": [{...}, {...}] }` but likewise it's not _required_. I would say it's more expandable if you nest it inside an object though, it allows you to add new properties onto the response later on, perhaps to support something like pagniation or something else. – Elliot Blackburn Aug 08 '19 at 13:13

1 Answers1

0

If you are planning to create a cross platform consumable REST API, I'd recommend you to use first example you recommended but with a difference.

Your json response for REST API should be in this form for general purpose;

{
    status: 200,
    message: "Data available.",
    data: [
        {data0...},
        {data1...}
    ],
    count: 2
}
  • Formatting your JSON response to a standart is important for cross platform support.
  • Platforms like Android and iOS requires object definitons like POJO example.
  • For better performance you can use count to define static arrays.
savrum
  • 203
  • 1
  • 6