How do you add an associated organization to a contact using REST API?

I have an existing contact and want to add an organization to the contact’s “Associated company(ies)”. I tried to use the following REST API endpoint:

/api/contacts

I used the PUT method to send the following body payload:

{
“id”: “307f4f05-6bb8-4827-bffc-9ea45ca927ae”,
“associatedOrganizations”: [
“a59aea58-43c9-441b-9947-a9e00afa9296”
]
}

This ended up completely deleting my contact. And it return a “204 No Content” response – apparently because the contact had gotten deleted and there was no contact content to return. The admin portal UI confirmed that it blew away my contact.

Two questions:

How do I modify the associatedOrganizations lists via REST API?

For that matter, how do I update any single contact field via the REST API?

Virto Commerce modules v3.829.0 added support for partial updates for entities using PATCH endpoint.

New methods were added to platform API:

PATCH api/members/{id}
PATCH api/contacts/{id}
PATCH api/organizations/{id}
All of them receive a special json object that describes the operations performed on the fields.

Example JSON Patch Request

[
  { "op": "add", "path": "/newField", "value": "New Value" },
  { "op": "remove", "path": "/oldField" },
  { "op": "replace", "path": "/existingField", "value": "Updated Value" },
  { "op": "move", "from": "/tempField", "path": "/finalField" },
  { "op": "copy", "from": "/sourceField", "path": "/destinationField" },
  { "op": "test", "path": "/testField", "value": "Expected Value" }
]

add - to add an element to an array
remove - to remove an element from an array
replace - to change value in the field

More about at the article JsonPatch in ASP.NET Core web API | Microsoft Learn

For example, to update the member’s “MiddleName” field, such a request is sufficient:

[
    {
        "op": "replace",
        "path": "/middleName",
        "value": "Mishel"
    }
]

To add new phone to the pones array (it is necessary to specify the index, it can be max as array size)

[
    {
        "op": "add",
        "path": "/phones/0",
        "value": "+79995558545"
    }
]

To remove

[
    {
        "op": "remove",
        "path": "/phones/1"
    }
]

Also it possible to add a new object to object-arrays

[
    {
        "op": "add",
        "path": "/addresses/1",
        "value": {
            "addressType": "BillingAndShipping",
            "name": "ESP, Valencia, c/ Eucaliptus 10",
            "organization": "",
            "countryCode": "ESP",
            "countryName": "Spain",
            "city": "Valencia",
            "postalCode": "44375",
            "zip": null,
            "line1": "c/ Eucaliptus 10",
            "line2": "pta. 10",
            "regionId": "",
            "regionName": null,
            "firstName": "Ivan",
            "middleName": null,
            "lastName": "Petrov",
            "phone": "",
            "email": "test@test.com",
            "outerId": null,
            "isDefault": false,
            "description": null
        }
    }
]

We recommend to use JSON patch builders to simplify usage of PATCH https://json-patch-builder-online.github.io/