JSON Style Guide
Apply Google's JSON Style Guide conventions for consistent, well-structured JSON APIs and data formats.
Overview
This skill provides guidelines for creating JSON APIs and data structures following Google's JSON Style Guide. The guide clarifies naming conventions, property structures, reserved property names, and standard patterns for JSON requests and responses in both RPC-based and REST-based APIs.
Core Principles
Use Double Quotes
All property names must be surrounded by double quotes. String values must use double quotes. Other value types (boolean, number, null, arrays, objects) should not be quoted.
json
{
"propertyName": "string value",
"count": 42,
"isActive": true,
"data": null
}
No Comments
Do not include comments in JSON objects. JSON does not support comments in the specification.
Flatten Data Appropriately
Data should be flattened unless there is a clear semantic reason for structured hierarchy. Group properties only when they represent a single logical structure.
Structured (preferred for related data):
json
{
"company": "Google",
"address": {
"line1": "111 8th Ave",
"city": "New York",
"state": "NY",
"zip": "10011"
}
}
Property Naming
Naming Format
Property names must:
- Be meaningful with defined semantics
- Use camelCase (not snake_case or PascalCase)
- Start with a letter, underscore (_), or dollar sign ($)
- Contain only letters, digits, underscores, or dollar signs
- Avoid JavaScript reserved keywords
json
{
"firstName": "John",
"lastName": "Doe",
"accountBalance": 1000.50,
"isVerified": true
}
Singular vs Plural
Use plural names for arrays. Use singular names for all other properties.
json
{
"author": "lisa",
"siblings": ["bart", "maggie"],
"totalItems": 10,
"itemCount": 10
}
JSON Maps vs Objects
When using a JSON object as a map (associative array), keys can use any Unicode characters. Map keys do not need to follow property naming guidelines.
json
{
"address": {
"addressLine1": "123 Anystreet",
"city": "Anytown"
},
"thumbnails": {
"72": "https://url.to.72px.thumbnail",
"144": "https://url.to.144px.thumbnail"
}
}
Property Values
Valid Value Types
Property values must be:
- Boolean: or
- Number: integers or floating-point
- String: Unicode strings in double quotes
- Object:
- Array:
- Null:
JavaScript expressions and functions are not allowed.
Empty or Null Values
Consider removing properties with empty or null values unless there is a strong semantic reason. A property with value
,
, or
may have semantic meaning and should be kept.
json
{
"volume": 10,
"balance": 0,
"currentlyPlaying": null
}
Better:
json
{
"volume": 10,
"balance": 0
}
Enum Values
Represent enums as strings (not numbers) to handle graceful changes as APIs evolve.
json
{
"color": "WHITE",
"status": "ACTIVE"
}
Standard Data Types
Dates
Format dates according to RFC 3339:
json
{
"lastUpdate": "2007-11-06T16:34:41.000Z",
"createdAt": "2024-02-19T10:30:00.000Z"
}
Time Durations
Format durations according to ISO 8601:
json
{
"duration": "P3Y6M4DT12H30M5S"
}
Latitude/Longitude
Format coordinates according to ISO 6709 using ±DD.DDDD±DDD.DDDD degrees format:
json
{
"location": "+40.6894-074.0447"
}
Standard JSON Structure
Top-Level Properties
A JSON response should have these optional top-level properties:
- - Version of the API (string)
- - Client-set value echoed by server (string)
- - Server-assigned response identifier (string)
- - Operation performed (string)
- - Input parameters for RPC requests (object)
- - Container for successful response data (object)
- - Error details if request failed (object)
A response should contain either
or
, but not both.
json
{
"apiVersion": "2.1",
"data": {
"kind": "user",
"id": "12345",
"name": "John Doe"
}
}
Data Object Properties
Common properties in the
object:
- - Type of object (should be first property)
- - Fields present in partial response
- - Entity tag for versioning
- - Unique identifier
- - Language code (BCP 47)
- - Last update timestamp (RFC 3339)
- - Boolean marker for deleted entries
- - Array of items (should be last property)
Pagination Properties
For paginated data in the
object:
- - Number of items in current response
- - Requested page size
- - Index of first item (1-based)
- - Total available items
- - Current page number (1-based)
- - Total number of pages
- - URI template for pagination
Link Properties
Link properties in the
object:
- / - Link to retrieve this resource
- / - Link to update/delete this resource
- / - Link to next page
- / - Link to previous page
Error Object Properties
When an error occurs, use the
object:
- - HTTP status code (integer)
- - Human-readable error message (string)
- - Array of error details (array)
Each error in
array can have:
- - Service identifier
- - Error type identifier
- - Detailed error message
- - Where error occurred
- - How to interpret location
- - URI to help documentation
- - URI to error report form
json
{
"apiVersion": "2.0",
"error": {
"code": 404,
"message": "File Not Found",
"errors": [{
"domain": "Calendar",
"reason": "ResourceNotFoundException",
"message": "File Not Found"
}]
}
}
Property Ordering
While property order is not enforced by JSON, certain orderings improve parsing efficiency:
- should be first - Helps parsers determine object type early
- should be last in - Allows reading collection metadata before parsing items
json
{
"data": {
"kind": "album",
"title": "My Photo Album",
"totalItems": 100,
"items": [
{
"kind": "photo",
"title": "My First Photo"
}
]
}
}
Quick Reference
Naming Checklist
Value Type Checklist
Structure Checklist
Additional Resources
Reference Files
For detailed specifications:
references/naming-conventions.md
- Complete naming rules and reserved keywords
references/reserved-properties.md
- Full list of reserved property names and their semantics
references/pagination-patterns.md
- Detailed pagination implementation patterns
Example Files
examples/api-response.json
- Standard API response structure
examples/error-response.json
- Error handling example
examples/paginated-response.json
- Pagination example with all properties