# Convert JSON values to match a schema

`POST /v1/convert/json/types/to/schema`

Fixes the type mismatches that make Parse JSON fail, such as Expected Integer but got Number or Expected String but got Integer. Numeric text becomes numbers, numbers and booleans become text, true and false text becomes booleans, and a missing nullable property becomes null. A value that cannot be converted safely (abc to integer, 42.5 to integer, null where null is not allowed) is left as it was and reported in failures. Every conversion is listed in changes.

## Request body

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| data | any | yes | The JSON document to fix, any JSON value |
| schema | object | yes | The schema, for example the one Parse JSON uses. Supported keywords: type (also a list), properties, required, items and nullable |

## Example request

A request body that works, as JSON.

```json
{
  "data": [
    {
      "city": "Porto",
      "temp_c": "17.5",
      "rain": "false"
    },
    {
      "city": "Lisboa",
      "temp_c": "21",
      "rain": "false"
    }
  ],
  "schema": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "temp_c": {
          "type": "number"
        },
        "rain": {
          "type": "boolean"
        }
      },
      "required": [
        "temp_c",
        "rain"
      ]
    }
  }
}
```

## Code samples

Replace YOUR_TOKEN with your access token.

### curl

```shell
curl -X POST 'https://api.skillfulsardine.com/v1/convert/json/types/to/schema' \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "data": [
    {
      "city": "Porto",
      "temp_c": "17.5",
      "rain": "false"
    },
    {
      "city": "Lisboa",
      "temp_c": "21",
      "rain": "false"
    }
  ],
  "schema": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "temp_c": {
          "type": "number"
        },
        "rain": {
          "type": "boolean"
        }
      },
      "required": [
        "temp_c",
        "rain"
      ]
    }
  }
}'
```

### PowerShell

```powershell
$body = @'
{
  "data": [
    {
      "city": "Porto",
      "temp_c": "17.5",
      "rain": "false"
    },
    {
      "city": "Lisboa",
      "temp_c": "21",
      "rain": "false"
    }
  ],
  "schema": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "temp_c": {
          "type": "number"
        },
        "rain": {
          "type": "boolean"
        }
      },
      "required": [
        "temp_c",
        "rain"
      ]
    }
  }
}
'@
$response = Invoke-RestMethod -Method Post -Uri 'https://api.skillfulsardine.com/v1/convert/json/types/to/schema' `
  -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 = {
    "data": [
        {
            "city": "Porto",
            "temp_c": "17.5",
            "rain": "false",
        },
        {
            "city": "Lisboa",
            "temp_c": "21",
            "rain": "false",
        },
    ],
    "schema": {
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "temp_c": {
                    "type": "number",
                },
                "rain": {
                    "type": "boolean",
                },
            },
            "required": [
                "temp_c",
                "rain",
            ],
        },
    },
}

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

### JavaScript

```javascript
const response = await fetch("https://api.skillfulsardine.com/v1/convert/json/types/to/schema", {
  method: "POST",
  headers: {
    "Authorization": "Bearer YOUR_TOKEN",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
  "data": [
    {
      "city": "Porto",
      "temp_c": "17.5",
      "rain": "false"
    },
    {
      "city": "Lisboa",
      "temp_c": "21",
      "rain": "false"
    }
  ],
  "schema": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "temp_c": {
          "type": "number"
        },
        "rain": {
          "type": "boolean"
        }
      },
      "required": [
        "temp_c",
        "rain"
      ]
    }
  }
}),
});
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/convert/json/types/to/schema", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${token}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
  "data": [
    {
      "city": "Porto",
      "temp_c": "17.5",
      "rain": "false"
    },
    {
      "city": "Lisboa",
      "temp_c": "21",
      "rain": "false"
    }
  ],
  "schema": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "temp_c": {
          "type": "number"
        },
        "rain": {
          "type": "boolean"
        }
      },
      "required": [
        "temp_c",
        "rain"
      ]
    }
  }
}),
});
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#"{
  "data": [
    {
      "city": "Porto",
      "temp_c": "17.5",
      "rain": "false"
    },
    {
      "city": "Lisboa",
      "temp_c": "21",
      "rain": "false"
    }
  ],
  "schema": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "temp_c": {
          "type": "number"
        },
        "rain": {
          "type": "boolean"
        }
      },
      "required": [
        "temp_c",
        "rain"
      ]
    }
  }
}"#)?;
    let response = reqwest::blocking::Client::new()
        .post("https://api.skillfulsardine.com/v1/convert/json/types/to/schema")
        .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/convert/json/types/to/schema")!)
request.httpMethod = "POST"
request.setValue("Bearer YOUR_TOKEN", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = Data(#"""
{
  "data": [
    {
      "city": "Porto",
      "temp_c": "17.5",
      "rain": "false"
    },
    {
      "city": "Lisboa",
      "temp_c": "21",
      "rain": "false"
    }
  ],
  "schema": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "temp_c": {
          "type": "number"
        },
        "rain": {
          "type": "boolean"
        }
      },
      "required": [
        "temp_c",
        "rain"
      ]
    }
  }
}
"""#.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 |
| --- | --- | --- | --- |
| data | any | yes | The document after conversion. Failed values are unchanged |
| changes | array of CoerceChange | yes | Every conversion that was made |
| failures | array of CoerceFailure | yes | Every value that could not be converted |
| changed_count | integer | yes | Number of conversions |
| failed_count | integer | yes | Number of failures |

## Example response

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

```json
{
  "data": {
    "data": [
      {
        "city": "Porto",
        "temp_c": 17.5,
        "rain": false
      },
      {
        "city": "Lisboa",
        "temp_c": 21,
        "rain": false
      }
    ],
    "changes": [
      {
        "path": "$[0].temp_c",
        "from_value": "17.5",
        "to_value": 17.5,
        "message": "Converted str to number"
      },
      {
        "path": "$[0].rain",
        "from_value": "false",
        "to_value": false,
        "message": "Converted str to boolean"
      },
      {
        "path": "$[1].temp_c",
        "from_value": "21",
        "to_value": 21,
        "message": "Converted str to number"
      },
      {
        "path": "$[1].rain",
        "from_value": "false",
        "to_value": false,
        "message": "Converted str to boolean"
      }
    ],
    "failures": [],
    "changed_count": 4,
    "failed_count": 0
  },
  "errors": null,
  "meta": {
    "request_id": "1db1ecde-643f-41a8-8910-3ebd9a59c123",
    "timestamp": "2026-10-27T07:19:42.041167Z",
    "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/coerce-json/