Question: Pros and Cons of using json array and json object


Pros and Cons of using json array and json object

Answers 1
Added at 2016-12-28 14:12

My question is little theoretical but it's interesting one. I want to know which approach is best while using web api.

I've two methods

  1. One has List<Customer> as parameter

public void Create(List<Customer>) { //... }

  1. Second has CustomerList as parameter (Where CustomerList class has property public List<Customer> customers {get; set;})

public void Create(CustomerList customer) { //... }

I know how for method 1, I need to pass json array and for method two I need to wrap json array to json object.

But my question is which approach is best and why?

Answers to

Pros and Cons of using json array and json object

nr: #1 dodano: 2016-12-28 14:12

When it comes to APIs, I always try to be as explicit as possible when it comes to the contract it exposes. So when you expect a List, use a JSON array. It will be much clearer to users of your API what is expected.

Having said that, the advantage of using a JSON object is that it won't be a breaking change if you decide to accept extra properties with your request later on.

In the end, it all depends on your use-case. Is this going to be an endpoint exposed to other people than just you? Do you already know you're going to want to accept additional data along with the customer list in the future? These are some of the questions you need to ask yourself.

Source Show
◀ Wstecz