I
I
Ilya Magdenko2020-06-22 12:53:22
API
Ilya Magdenko, 2020-06-22 12:53:22

Is it correct to make API JSON Body with optional keys?

Hello,

The question is to understand whether it is right to do such an API design or do I need to use other principles? (If others, which ones?)

I send an API call with a JSON Body in which I have the following keys:

{
"order_id"
"customer_id"
"patient_age"
"referral_reasons"
"patient_gender"
}


I want to make only 2 keys required:
"order_id"
"customer_id"


And the rest will be sent at the request of the sender. Sometimes there will be values ​​for all keys, sometimes only for certain ones. But there will always be 2 required keys/values.

Is it correct to do this or is there a more elegant solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Developer, 2020-06-22
@duntik

In general, both options are appropriate.
But. It is often more convenient for the front to have the back pass all the fields, and empty ones would be marked as null.
It depends on the implementation at the front. In general, as agreed.
I am for the minimum option so that unnecessary fields are not transferred, but only required ones.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question