REST-API Json structure for patching nested data

57 viewsapidata structureshttpjsonrest
0

Let’s assume you have an API endpoint that returns user data with expanded country data:

GET user/1

{
    UserName: "John Miller",
    Country: {
        Id: 1,
        Name: "Switzerland"
    }
}

Now you can also modify user data with the same endpoint.
I see two possibilities for the structure to do this.

PATCH user/1

{
    UserName: "John Miller",
    Country: {
        Id: 2
    }
}

PATCH user/2

{
    UserName: "John Miller",
    CountryId: 2
}

The first version will keep the structure, but only require the id. The second one will not be nested and directly contain the id.

Is there any best practise or guideline how to do this?