Vitalii Ivanov
Vitalii Ivanov

Reputation: 752

JSON request to create entity with existent inner object

I develop rest api and think about following task. I need to post a vehicle entity and bind existing driver to it. What is the common approach to format json for such request? I choose one of these variants, but maybe there are better solutions:

1. {"model":"Corolla","number":"12345", "driver": {"id": 5}}

2. {"model":"Corolla","number":"12345", "driver": {"id": 5, "name": "John"}}

3. {"model":"Corolla","number":"12345", "driverId": 5} 

The question is about how to pass information about already created driver entity with known id.

Upvotes: 0

Views: 55

Answers (2)

inf3rno
inf3rno

Reputation: 26137

You have to use a hypermedia type, e.g. with HAL+JSON you do it this way:

{
    "model":"Corolla",
    "number":"12345",
    "_embedded": {
        "driver": {
            "name": "John",
            "_links": {
                "self": {
                    "href": "/drivers/5"
                }
            }
        }
    },
    "_links": {
        "self": {
            "href": "/models/corolla"
        }
    }
}

Upvotes: 2

ToYonos
ToYonos

Reputation: 16833

According to jsonapi, this could be a good format :

{
    "model":"Corolla",
    "number":"12345"
    "links":
    {
        "driver": "5"
    }
}

Conventions defined in this website may be usefull for you.

Upvotes: 1

Related Questions