2026-08-06 17:28:56.635287+08 / restful-api

Data Formats

Guidelines for JSON, binary data, media types, standard property formats, date and time, numeric precision, and international codes.

Restful ApiApi DesignOpenapi

Data Formats

Data format design is a critical part of API design. A well-defined data format improves interoperability, backward compatibility, and long-term API evolution.

This section defines guidelines for:

  • JSON data encoding
  • Binary data handling
  • Media type usage
  • Standard property formats
  • Date and time representation
  • Number precision definition
  • Country, language, and currency codes

8.1 Use JSON for Structured Data

JSON SHOULD be used as the default format for transferring structured data between API clients and servers.

JSON provides:

  • Broad language support
  • Simple parsing
  • Good compatibility with OpenAPI specifications
  • Easy interoperability between different platforms

Example:

http
GET /users/123

Accept: application/json

Response:

```json
{
  "id": "123",
  "name": "Alice",
  "email": "alice@example.com"
}

JSON payloads SHOULD follow standard JSON specifications:

  • UTF-8 encoding MUST be used.
  • Strings MUST contain valid Unicode characters.
  • Object property names MUST be unique.

Example of valid JSON:

json
{
  "id": 1001,
  "name": "Alice"
}

Example of invalid JSON:

json
{
  "id": 1001,
  "id": 1002
}

Duplicate property names may lead to different parsing results between clients.


8.2 Use Non-JSON Media Types for Binary Data

JSON is not suitable for all types of data.

Alternative media types SHOULD be used when transferring:

  • Images
  • Documents
  • Videos
  • Binary files
  • Data that does not need to be interpreted by clients

Example:

http
GET /users/123/avatar

Accept: image/png

Response:

text
Content-Type: image/png

Avoid embedding binary content directly into JSON:

json
{
  "image": "base64 encoded data"
}

Advantages of using dedicated media types:

  • Smaller payload size
  • Lower encoding and decoding overhead
  • Better streaming support

8.3 Encode Embedded Binary Data Using base64url

Binary data SHOULD generally not be embedded inside JSON documents.

The preferred approach is to expose binary content using dedicated media types:

text
Client
  |
  |
  v

image/png
application/pdf
application/octet-stream

If binary data must be embedded in JSON, it MUST be encoded using:

text
base64url

Example:

json
{
  "fileName": "document.pdf",
  "content": "JVBERi0xLjQK..."
}

base64url differs from standard Base64:

Standard Base64 base64url
+ -
/ _

base64url avoids characters that may cause problems in URLs.


8.4 Prefer Standard Media Types

APIs SHOULD use standardized media types whenever possible.

Recommended:

text
application/json

Avoid proprietary media types:

text
application/x-company-json
application/custom-data

Standard media types provide:

  • Better tooling support
  • Easier client implementation
  • Improved interoperability

For API errors, the standardized format SHOULD be used:

text
application/problem+json

Example:

json
{
  "type": "https://example.com/errors/not-found",
  "title": "Resource not found",
  "status": 404,
  "detail": "The requested resource does not exist"
}

8.5 Use Standard Property Formats

API properties SHOULD use standardized formats defined by OpenAPI and JSON Schema.

Common formats include:

Type Format Example
integer int32 12345
integer int64 772107100456824
number float 3.1415927
number double 3.141592653589793
number decimal 99.95
string date "2026-08-06"
string date-time "2026-08-06T10:30:00Z"
string email "user@example.com"
string uuid "550e8400-e29b-41d4-a716"
string uri "https://example.com"
string ipv4 "192.168.1.10"
string ipv6 "2001:db8::1"

Example:

yaml
properties:

  createdAt:
    type: string
    format: date-time

  userId:
    type: string
    format: uuid

  email:
    type: string
    format: email

8.6 Use Standard Date and Time Formats

Date and time values MUST use standardized formats.

The recommended standard is:

text
RFC 3339

Date

Format:

text
YYYY-MM-DD

Example:

json
{
  "birthday": "1990-01-01"
}

Date-Time

Format:

text
YYYY-MM-DDTHH:mm:ssZ

Example:

json
{
  "createdAt": "2026-08-06T10:30:00Z"
}

Avoid custom formats:

json
{
  "createdAt": "06/08/2026 10:30"
}

Reasons:

  • Date formats differ between regions.
  • Time zone interpretation may become ambiguous.

8.7 Use Standard Country, Language and Currency Codes

APIs SHOULD use internationally recognized standards for country, language, and currency codes.

Recommended standards:

Category Standard Example
Country ISO 3166-1 alpha-2 US
Language ISO 639-1 en
Language Variant BCP 47 en-US
Currency ISO 4217 USD

Example:

json
{
  "country": "US",
  "language": "en-US",
  "currency": "USD"
}

Avoid custom values:

json
{
  "country": "America",
  "currency": "Dollar"
}

8.8 Define Format for Number and Integer Types

API specifications MUST define the format for all numeric properties.

Without an explicit format, clients may incorrectly determine the precision and cause unintended data changes.

Avoid:

yaml
amount:
  type: number

Because clients cannot know whether the value represents:

  • float
  • double
  • decimal

Integer Formats

Type Format Description
integer int32 Signed 32-bit integer
integer int64 Signed 64-bit integer
integer bigint Arbitrary precision integer

Example:

yaml
orderCount:
  type: integer
  format: int32

Number Formats

Type Format Description
number float IEEE 754 binary32
number double IEEE 754 binary64
number decimal Arbitrary precision decimal

For financial values, use decimal:

yaml
amount:
  type: number
  format: decimal
  example: 99.95

Avoid:

yaml
amount:
  type: number
  format: float

because floating-point numbers may introduce precision errors.

Example:

text
0.1 + 0.2 != 0.3

8.9 OpenAPI Example

yaml
components:

  schemas:

    Money:
      type: object
      properties:

        amount:
          type: number
          format: decimal
          description: Amount expressed in major currency units
          example: 99.95


    OrderList:
      type: object
      properties:

        pageSize:
          type: integer
          format: int32
          description: Number of items returned in the list
          example: 50

Summary Checklist

Area Recommendation
Structured data Use JSON
Encoding UTF-8
Binary data Use dedicated media types
Embedded binary Use base64url
JSON media type application/json
Error format application/problem+json
Date format RFC 3339
Country code ISO 3166
Language code ISO 639 / BCP 47
Currency code ISO 4217
Integer values Always define format
Number values Always define precision
Data Formats | Remi Resume