<@U04UQ68083H> What is the different between “add”...
# ory-copilot
m
@chilly-king-10285 What is the different between “add” and “replace” in the patchIdentity endpoint? They do seem to do both the same thing
c
A message from kapa.ai
The
patchIdentity
endpoint uses JSON Patch to partially update an identity's field. The difference between "add" and "replace" operations in JSON Patch is as follows: - "add": This operation is used to add a new key-value pair to the target object or insert a new value into an array. If the target location already contains a value, the new value will overwrite the existing value. - "replace": This operation is used to replace an existing value at the target location with a new value. In the context of the
patchIdentity
endpoint, you would use "add" if you want to add a new field to the identity or insert a value into an array, and "replace" if you want to update the value of an existing field. Here's the relevant extract from the documentation:
Copy code
oprequired | string The operation to be performed. One of "add", "remove", "replace", "move", "copy", or "test".
Source: [API Reference > kratos > reference > api > identity > Patch an Identity](https://www.ory.sh/docs/kratos/reference/api#tag/identity/operation/patchIdentity)
m
Well this:
Copy code
[
    {
        "op": "add",
        "path": "/metadata_public",
        "value": {"foo": "bar"}
    }
]
and this does exactly the same thing:
Copy code
[
    {
        "op": "replace",
        "path": "/metadata_public",
        "value": {"foo": "bar"}
    }
]
@chilly-king-10285 I want to set add a new field user_id in metadata_public when metadata_public exists already and create metadata_public with user_id when it does not exist in an identity
c
A message from kapa.ai
To add a new field
user_id
in
metadata_public
when it already exists or create
metadata_public
with
user_id
when it doesn't exist, you can use the
patchIdentity
endpoint with the "add" operation. Here's an example of how to do this: 1. First, retrieve the identity using the
GET /admin/identities/{id}
endpoint to check if
metadata_public
exists or not. Source: [API Reference > kratos > reference > api > identity > Get an Identity](https://www.ory.sh/docs/kratos/reference/api#operation/getIdentity) 2. If
metadata_public
exists, use the "add" operation to add the
user_id
field to
metadata_public
. If
metadata_public
doesn't exist, use the "add" operation to create
metadata_public
with the
user_id
field. Here's an example JSON Patch request for adding or updating the
user_id
field in `metadata_public`:
Copy code
json [   {     "op": "add",     "path": "/metadata_public/user_id",     "value": "your_user_id_value"   } ]
Send this JSON Patch request to the
PATCH /admin/identities/{id}
endpoint to update the identity. Source: [API Reference > kratos > reference > api > identity > Patch an Identity](https://www.ory.sh/docs/kratos/reference/api#operation/patchIdentity)
m
Thats annoying, isn’t there an option that creates the
metadata_public
object when it does not exist 😄
b
Unfortunately not, AFAIK. But that should probably be considered a bug.