Skip to content
Back to Learn
·6 min read

JSON and GraphQL: How They Work Together

JSON and GraphQL share a symbiotic relationship. While GraphQL is a query language and runtime, JSON is the wire format that carries queries, responses, and schema definitions. Understanding how JSON integrates with GraphQL is essential for building efficient, type-safe APIs. This guide covers GraphQL JSON queries, response formats, schema definitions, variable serialization, and best practices. Use our JSON to GraphQL Schema generator to convert JSON samples to GraphQL types.

How JSON and GraphQL Work Together

GraphQL ComponentJSON RoleExample
QueriesSent as JSON over HTTP POST{"query": "..."}
VariablesJSON object with variable values{"variables": {"id": 1}}
ResponsesJSON with data/errors structure{"data": {...}, "errors": [...]}
SchemaSDL (not JSON), but can be described as JSONIntrospection returns JSON
IntrospectionJSON response describing the schema{"__schema": {...}}

GraphQL Request as JSON

// GraphQL HTTP POST body (JSON)
POST /graphql
Content-Type: application/json

{
  "query": "query GetUser($id: ID!, $includePosts: Boolean) { user(id: $id) { id name email posts @include(if: $includePosts) { title createdAt } } }",
  "variables": {
    "id": "42",
    "includePosts": true
  },
  "operationName": "GetUser"
}

// Simplified query without variables
{
  "query": "{ user(id: 42) { id name email } }"
}

GraphQL Response Format

// Successful response
{
  "data": {
    "user": {
      "id": "42",
      "name": "Alice",
      "email": "alice@example.com",
      "posts": [
        { "title": "GraphQL Guide", "createdAt": "2025-01-15T10:00:00Z" }
      ]
    }
  }
}

// Response with errors (partial data)
{
  "data": {
    "user": null
  },
  "errors": [
    {
      "message": "User not found",
      "locations": [{ "line": 2, "column": 5 }],
      "path": ["user"],
      "extensions": {
        "code": "NOT_FOUND",
        "userId": "999"
      }
    }
  ]
}

Converting JSON to GraphQL Schema

// Sample JSON data
{
  "id": 1,
  "name": "Alice",
  "email": "alice@example.com",
  "age": 30,
  "address": {
    "city": "New York",
    "zip": "10001"
  },
  "tags": ["developer", "graphql"]
}

// Generated GraphQL Schema
// type Address {
//   city: String!
//   zip: String!
// }
//
// type User {
//   id: Int!
//   name: String!
//   email: String!
//   age: Int
//   address: Address
//   tags: [String!]
// }
//
// type Query {
//   user(id: Int!): User
// }

Use our JSON to GraphQL Schema tool to automatically convert your JSON samples to GraphQL type definitions.

GraphQL Variables as JSON

// Variables must match the query's variable definitions
// Query:
// mutation CreateUser($input: UserInput!) {
//   createUser(input: $input) { id name }
// }

// Variables (JSON):
{
  "input": {
    "name": "Bob",
    "email": "bob@example.com",
    "age": 25,
    "address": {
      "city": "San Francisco",
      "zip": "94105"
    }
  }
}

// Full request:
{
  "query": "mutation CreateUser($input: UserInput!) { createUser(input: $input) { id name } }",
  "variables": {
    "input": {
      "name": "Bob",
      "email": "bob@example.com",
      "age": 25
    }
  }
}

Apollo Client: JSON Caching

import { ApolloClient, InMemoryCache, gql } from '@apollo/client';

const client = new ApolloClient({
  uri: '/graphql',
  cache: new InMemoryCache({
    typePolicies: {
      User: {
        fields: {
          // Custom merge for paginated fields
          posts: {
            merge(existing = [], incoming) {
              return [...existing, ...incoming];
            }
          }
        }
      }
    }
  })
});

// The cache stores normalized JSON objects
// Each type+id combination is stored once (normalization)
// This allows consistent updates across components

GraphQL vs REST JSON Comparison

FeatureREST JSONGraphQL JSON
Request formatURL + optional JSON bodyJSON with query + variables
Response structureEnvelope (status, data, meta)Standardized data + errors
Over-fetchingCommon — returns all fieldsRare — client specifies fields
Under-fetchingCommon — multiple endpoints neededRare — nested queries in one request
VersioningURL or header basedSchema evolution via deprecation
CachingHTTP caching (URL-based)Client-side normalization cache
File uploadmultipart/form-dataGraphQL multipart request spec

Next Steps

Generate GraphQL schemas from your JSON with JSON to GraphQL Schema. Generate GraphQL queries with JSON to GraphQL Query. Convert GraphQL schemas back to JSON with GraphQL Schema to JSON.