# Merge a partial update into a JSON document

`POST /v1/merge/json/with/json`

Deep merge: objects merge key by key and the patch wins for everything else. Arrays are replaced by default; choose concat to append, or merge_by_key with array_key to merge objects that share a key value. A null in the patch writes null, or deletes the key with null_removes.

## Request body

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| base | any | yes | The document to update |
| patch | any | yes | The changes to apply on top |
| arrays | one of: replace, concat, merge_by_key | no | replace, concat or merge_by_key (default `"replace"`) |
| array_key | string | no | For merge_by_key: the property that identifies an object |
| null_removes | boolean | no | True makes a null in the patch delete the key. False writes the null (default `false`) |

## Example request

A request body that works, as JSON.

```json
{
  "base": {
    "name": "Ana",
    "city": "Porto",
    "items": [
      {
        "id": "A1",
        "qty": 2
      }
    ]
  },
  "patch": {
    "city": "Lisboa",
    "items": [
      {
        "id": "A2",
        "qty": 1
      }
    ]
  },
  "arrays": "replace",
  "array_key": "id"
}
```

## Code samples

Replace YOUR_TOKEN with your access token.

### curl

```shell
curl -X POST 'https://api.skillfulsardine.com/v1/merge/json/with/json' \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "base": {
    "name": "Ana",
    "city": "Porto",
    "items": [
      {
        "id": "A1",
        "qty": 2
      }
    ]
  },
  "patch": {
    "city": "Lisboa",
    "items": [
      {
        "id": "A2",
        "qty": 1
      }
    ]
  },
  "arrays": "replace",
  "array_key": "id"
}'
```

### PowerShell

```powershell
$body = @'
{
  "base": {
    "name": "Ana",
    "city": "Porto",
    "items": [
      {
        "id": "A1",
        "qty": 2
      }
    ]
  },
  "patch": {
    "city": "Lisboa",
    "items": [
      {
        "id": "A2",
        "qty": 1
      }
    ]
  },
  "arrays": "replace",
  "array_key": "id"
}
'@
$response = Invoke-RestMethod -Method Post -Uri 'https://api.skillfulsardine.com/v1/merge/json/with/json' `
  -Headers @{ Authorization = 'Bearer YOUR_TOKEN' } `
  -ContentType 'application/json; charset=utf-8' `
  -Body ([System.Text.Encoding]::UTF8.GetBytes($body))
$response | ConvertTo-Json -Depth 10
```

### Python

```python
import requests

payload = {
    "base": {
        "name": "Ana",
        "city": "Porto",
        "items": [
            {
                "id": "A1",
                "qty": 2,
            },
        ],
    },
    "patch": {
        "city": "Lisboa",
        "items": [
            {
                "id": "A2",
                "qty": 1,
            },
        ],
    },
    "arrays": "replace",
    "array_key": "id",
}

response = requests.post(
    "https://api.skillfulsardine.com/v1/merge/json/with/json",
    headers={"Authorization": "Bearer YOUR_TOKEN"},
    json=payload,
    timeout=30,
)
print(response.json())
```

### JavaScript

```javascript
const response = await fetch("https://api.skillfulsardine.com/v1/merge/json/with/json", {
  method: "POST",
  headers: {
    "Authorization": "Bearer YOUR_TOKEN",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
  "base": {
    "name": "Ana",
    "city": "Porto",
    "items": [
      {
        "id": "A1",
        "qty": 2
      }
    ]
  },
  "patch": {
    "city": "Lisboa",
    "items": [
      {
        "id": "A2",
        "qty": 1
      }
    ]
  },
  "arrays": "replace",
  "array_key": "id"
}),
});
const result = await response.json();
console.log(result);
```

### Node

```javascript
// Node 18 or later. Save as call.mjs and run: API_TOKEN=... node call.mjs
const token = process.env.API_TOKEN;

const response = await fetch("https://api.skillfulsardine.com/v1/merge/json/with/json", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${token}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
  "base": {
    "name": "Ana",
    "city": "Porto",
    "items": [
      {
        "id": "A1",
        "qty": 2
      }
    ]
  },
  "patch": {
    "city": "Lisboa",
    "items": [
      {
        "id": "A2",
        "qty": 1
      }
    ]
  },
  "arrays": "replace",
  "array_key": "id"
}),
});
const result = await response.json();
console.log(result);
```

### Rust

```rust
// Cargo.toml: reqwest = { version = "0.12", features = ["blocking", "json"] }, serde_json = "1"
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let body: serde_json::Value = serde_json::from_str(r#"{
  "base": {
    "name": "Ana",
    "city": "Porto",
    "items": [
      {
        "id": "A1",
        "qty": 2
      }
    ]
  },
  "patch": {
    "city": "Lisboa",
    "items": [
      {
        "id": "A2",
        "qty": 1
      }
    ]
  },
  "arrays": "replace",
  "array_key": "id"
}"#)?;
    let response = reqwest::blocking::Client::new()
        .post("https://api.skillfulsardine.com/v1/merge/json/with/json")
        .bearer_auth("YOUR_TOKEN")
        .json(&body)
        .send()?;
    println!("{}", response.text()?);
    Ok(())
}
```

### Swift

```swift
import Foundation

var request = URLRequest(url: URL(string: "https://api.skillfulsardine.com/v1/merge/json/with/json")!)
request.httpMethod = "POST"
request.setValue("Bearer YOUR_TOKEN", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = Data(#"""
{
  "base": {
    "name": "Ana",
    "city": "Porto",
    "items": [
      {
        "id": "A1",
        "qty": 2
      }
    ]
  },
  "patch": {
    "city": "Lisboa",
    "items": [
      {
        "id": "A2",
        "qty": 1
      }
    ]
  },
  "arrays": "replace",
  "array_key": "id"
}
"""#.utf8)

let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
```

## Response

The fields inside `data` of a successful response.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| result | any | yes | The base with the patch applied. The inputs are not modified |
| changed | boolean | yes | False when the patch changed nothing |

## Example response

A real answer from the operation, as JSON. The fields in the table above are inside `data`.

```json
{
  "data": {
    "result": {
      "name": "Ana",
      "city": "Lisboa",
      "items": [
        {
          "id": "A2",
          "qty": 1
        }
      ]
    },
    "changed": true
  },
  "errors": null,
  "meta": {
    "request_id": "d60396c1-c633-4231-803c-df7df1c8c166",
    "timestamp": "2026-10-27T07:19:42.155640Z",
    "version": "2026-03-26"
  },
  "pagination": null
}
```

## Responses

| Status | When it happens |
| --- | --- |
| 200 | Successful Response |
| 400 | The body respects the operation schema, but the use case rejected it. Each problem is one ApiError entry |
| 413 | The request is larger than the request size limit of the API allows |
| 422 | The body does not respect the operation schema. Each problem is one ApiError entry with the code validation_error |
| 500 | Something failed inside the server and was not an input problem. The same envelope, errors may be empty |

---
Source: https://skillfulsardine.com/developers/api/merge-json/