Revision 0.9
Each style point has a summary for which additional information is available by toggling the accompanying arrow button that looks this way:
. You may toggle all summaries with the big arrow button:This style guide documents guidelines and recommendations for building JSON APIs at Google. In general, JSON APIs should follow the spec found at JSON.org. This style guide clarifies and standardizes specific cases so that JSON APIs from Google have a standard look and feel. These guidelines are applicable to JSON requests and responses in both RPC-based and REST-based APIs.
For the purposes of this style guide, we define the following terms:
{ // The name/value pair together is a "property". "propertyName": "propertyValue" }
Javascript's number
type encompasses all floating-point numbers, which is a broad designation. In this guide, number
will refer to JavaScript's number
type, while integer
will refer to integers.
null
.
As mentioned above, property value types must be booleans, numbers, strings, objects, arrays, or null
. However, it is useful define a set of standard data types when dealing with certain values. These data types will always be strings, but they will be formatted in a specific manner so that they can be easily parsed.
In order to maintain a consistent interface across APIs, JSON objects should follow the structure outlined below. This structure applies to both requests and responses made with JSON. Within this structure, there are certain property names that are reserved for specific uses. These properties are NOT required; in other words, each reserved property may appear zero or one times. But if a service needs these properties, this naming convention is recommend. Here is a schema of the JSON structure, represented in Orderly format (which in turn can be compiled into a JSONSchema). You can few examples of the JSON structure at the end of this guide.
object { string apiVersion?; string context?; string id?; string method?; object { string id? }* params?; object { string kind?; string fields?; string etag?; string id?; string lang?; string updated?; # date formatted RFC 3339 boolean deleted?; integer currentItemCount?; integer itemsPerPage?; integer startIndex?; integer totalItems?; integer pageIndex?; integer totalPages?; string pageLinkTemplate /^https?:/ ?; object {}* next?; string nextLink?; object {}* previous?; string previousLink?; object {}* self?; string selfLink?; object {}* edit?; string editLink?; array [ object {}*; ] items?; }* data?; object { integer code?; string message?; array [ object { string domain?; string reason?; string message?; string location?; string locationType?; string extendedHelp?; string sendReport?; }*; ] errors?; }* error?; }*;
The JSON object has a few top-level properties, followed by either a data
object or an error
object, but not both. An explanation of each of these properties can be found below.
The top-level of the JSON object may contain the following properties.
The data
property of the JSON object may contain the following properties.
data (or any child element)
The following properties are located in the data
object, and help page through a list of items. Some of the language and concepts are borrowed from the OpenSearch specification.
The paging properties below allow for various styles of paging, including:
nextLink
and previousLink
properties (described in the "Reserved Property Names for Links" section below) are used for this style of paging.?startIndex=200
.?page=1
or ?page=20
. The pageIndex
and totalPages
properties are used for this style of paging.An example of how to use these properties to implement paging can be found at the end of this guide.
The following properties are located in the data
object, and represent references to other resources. There are two forms of link properties: 1) objects, which can contain any sort of reference (such as a JSON-RPC object), and 2) URI strings, which represent URIs to resources (and will always be suffixed with "Link").
The error
property of the JSON object may contain the following properties.
Properties can be in any order within the JSON object. However, in some cases the ordering of properties can help parsers quickly interpret data and lead to better performance. One example is a pull parser in a mobile environment, where performance and memory are critical, and unnecessary parsing should be avoided.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License.
Revision 0.9