GraphQL to JSON Body Converter

Enter a GraphQL query to convert it to a JSON body for a POST request.

GraphQL Query
GraphQL Variables
JSON Body

Why Convert GraphQL Query to JSON Body?

If you're connecting to a GraphQL server using HTTPS requests, the server probably expects you to send a POST request with the query in the JSON body like this:

{
  "query": "query { countries { code name capital continent { name } }}"
}
So you need your GraphQL query to be in this format to send it to the server.


Unfortunately, most GraphQL API docs/ playgrounds just give you straight GraphQL queries like this:

query {
  countries {
    code
    name
    capital
    continent {
      name
    }
  }
}
You cannot use this directly in the request body as it is not valid JSON.

Converting a GraphQL query to a JSON body is simple. First, we remove new lines/ extra spaces from the GraphQL query. Then we add it under a "query" key in a JSON object. We also add any variables under the "variables" key in the JSON. This is what the converter above does!

You might want to add the optional "operationName" field. You can add this field to the JSON yourself if you need it.

{
  "query": "query { countries { code name capital { name } } }",
  "operationName": "MY OPERATION NAME",
  "variables": { "code": "Gb" }
}

Convert GraphQL Query to JSON Body Online

  1. Paste your GraphQL query into the "GraphQL Query" input above.
  2. Add variables as JSON in the "GraphQL Variables" input above. This is optional.
  3. Your query will automatically be converted to a JSON body in the right hand input.
  4. Click the 'Copy' button to copy the JSON body to your clipboard.

Having trouble? Try clicking the "Beautify" button to validate and format your query. You will be shown any errors.