JSON to GraphQL Query Generator

Convert JSON data to GraphQL query or mutation strings with nested field selection and variables.

What is JSON to GraphQL Query Generator?

GraphQL queries declare the exact shape of data you want from a server, using nested field selections that mirror the response structure. This tool reverses that process — it takes a JSON response and generates the corresponding GraphQL query or mutation string. Nested objects become nested field selections with curly braces, arrays become list fields using the first element as a template, and primitive values become leaf fields. The generator also extracts variable declarations with proper GraphQL types (String, Int, Float, Boolean) for parameterized operations.

How to Use

  1. Set the operation name in the options bar to match your API naming conventions (e.g., GetUser, CreateOrder)
  2. Choose between Query (for reading data) and Mutation (for modifying data) using the type dropdown
  3. Paste your JSON response data into the input area — the structure determines the field selections
  4. Click "Generate" to create the GraphQL query or mutation string with nested selections
  5. Copy the output and use it in your GraphQL client (Apollo, urql, Relay, or plain fetch)

Why Use This Tool?

Automatically generates nested field selections from JSON structure — no manual query writing needed
Supports both query and mutation operation types for reading and modifying data
Generates variable declarations with proper GraphQL types for parameterized, reusable operations
Handles deeply nested objects and arrays with correct indentation and brace nesting
Produces commented variable JSON for easy testing in GraphiQL or Apollo Explorer
No data leaves your browser — all processing is client-side for security

Tips & Best Practices

  • Use realistic sample data for the most accurate query generation — the tool uses the first array element as the selection template
  • Nested JSON objects become nested GraphQL field selections with proper indentation
  • Arrays are treated as list fields — only the first element is used as the selection template
  • Customize the operation name to match your API conventions (e.g., GetUser instead of GetData)
  • After generating, review the query and add arguments, directives, or fragments as needed for your specific API

Frequently Asked Questions

How are JSON structures converted to GraphQL queries?

JSON objects become nested field selections with curly braces, arrays become list fields using the first element as a template for the selection set, and primitive values (strings, numbers, booleans) become leaf fields. The tool preserves the nesting depth and field names from the original JSON structure.

What are GraphQL variables and why are they generated?

Variables allow you to parameterize your GraphQL operations. Instead of hardcoding values, you declare them as variables ($varName: Type) in the operation signature and pass them separately. This makes queries reusable, cacheable, and prevents injection attacks. The tool generates variable declarations based on the primitive values found in your JSON.

When should I NOT use this tool for GraphQL query generation?

Avoid this tool when you need to generate queries with arguments, aliases, fragments, directives, or subscriptions — these GraphQL features require manual query writing. Also, the tool uses the first array element as a template, so if your array elements have different shapes, some fields may be missing from the generated query.

Can I generate mutations?

Yes. Select "Mutation" from the type dropdown. The structure is the same as queries, but the operation keyword changes from "query" to "mutation". Mutations are typically used for creating, updating, or deleting data on the server.

How are JSON types mapped to GraphQL types?

JSON strings map to String, integers map to Int, floats map to Float, booleans map to Boolean, null maps to String (as a fallback), arrays map to [Type] where Type is inferred from the first element, and objects map to nested field selections without a type declaration.

Is my data sent to a server?

No. All query generation happens entirely in your browser using client-side JavaScript. Your JSON data never leaves your device, so you can safely generate queries from real API responses containing sensitive data.

Real-world Examples

User Profile Query

Generate a GraphQL query from a user profile JSON response for fetching the same data structure.

Input
{"user": {"id": 1, "name": "Alice", "email": "[email protected]", "posts": [{"id": 1, "title": "Hello"}]}}
Output
query GetUser($user_id: Int, $user_name: String, $user_email: String, $user_posts_id: Int, $user_posts_title: String) {
  user {
    id
    name
    email
    posts {
      id
      title
    }
  }
}

Create Order Mutation

Generate a GraphQL mutation from an order JSON payload for submitting new orders.

Input
{"order": {"product": "Widget", "quantity": 5, "price": 9.99}}
Output
mutation CreateOrder($order_product: String, $order_quantity: Int, $order_price: Float) {
  order {
    product
    quantity
    price
  }
}

Related Tools