JSON Serialization
JSON is the preferred serialization format for GraphQL, though as noted above, GraphQL does not require a specific serialization format. For consistency and ease of notation, examples of the response are given in JSON throughout the spec. In particular, in our JSON examples, we will represent primitives using the following JSON concepts:
GraphQL Value | JSON Value |
---|---|
Map | Object |
List | Array |
Null | null |
String | String |
Boolean | trueorfalse |
Int | Number |
Float | Number |
Enum Value | String |
Object Property Ordering
While JSON Objects are specified as anunordered collection of key‐value pairsthe pairs are represented in an ordered manner. In other words, while the JSON strings{ "name": "Mark", "age": 30 }
and{ "age": 30, "name": "Mark" }
encode the same value, they also have observably different property orderings.
Since the result of evaluating a selection set is ordered, the JSON object serialized should preserve this order by writing the object properties in the same order as those fields were requested as defined by query execution.
For example, if the query was{ name, age }
, a GraphQL server responding in JSON should respond with{ "name": "Mark", "age": 30 }
and should not respond with{ "age": 30, "name": "Mark" }
.
This does not violate the JSON spec, as clients may still interpret objects in the response as unordered Maps and arrive at a valid value.