Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

DoH JSON: 5 Differences Between Cloudflare and Google

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide explains how to read the JSON responses from DNS-over-HTTPS (DoH) services and highlights five key differences between Cloudflare's and Google's implementations. You'll learn the structure of the responses, what each field means, and how the two providers differ in their JSON schemas, cache behavior, and error handling. No prior experience with DoH is required, but familiarity with JSON and basic DNS concepts will help.

What You'll Need

  • A web browser or a tool like curl to make HTTPS requests.
  • Basic understanding of DNS records (A, AAAA, MX, etc.).
  • Optional: a JSON viewer or jq for pretty-printing responses.

Step-by-Step

  1. Make a DoH query to Cloudflare. Use the endpoint https://cloudflare-dns.com/dns-query with the parameter ?name=example.com&type=A. Include the header Accept: application/dns-json to get JSON. For example, in a terminal: curl -H 'Accept: application/dns-json' 'https://cloudflare-dns.com/dns-query?name=example.com&type=A'. The response will be a JSON object with fields like Status, Answer, and Question.
  2. Make the same query to Google. Use https://dns.google/resolve with the same parameters and header. For example: curl -H 'Accept: application/dns-json' 'https://dns.google/resolve?name=example.com&type=A'. The response will look similar but with some differences you'll spot in the following steps.
  3. Compare the top-level fields. Both return a Status field (the DNS response code, e.g., 0 for NOERROR), Question (the query echoed back), and Answer (array of records). However, Cloudflare includes a flags field (e.g., flags: ["qr", "rd", "ra"]) that Google does not. Google includes Comment in some error responses (e.g., when a domain doesn't exist), which Cloudflare does not.
  4. Examine the Answer array structure. Each answer object in both services contains name, type, TTL, and data. The data field holds the record value (e.g., an IP address). One difference: Cloudflare returns the TTL as an integer (e.g., 300), while Google also returns it as an integer, but Google includes an additional expiration field (a timestamp when the record expires) in some cases, which Cloudflare omits.
  5. Check the Status codes and error handling. Both use standard DNS response codes. However, when a query fails (e.g., NXDOMAIN), Google returns a Status of 3 and includes a Comment field like "No such name". Cloudflare returns the same Status but does not include a Comment. Additionally, for malformed queries, Google returns a JSON with Status: 0 and an empty Answer, while Cloudflare returns an HTTP 400 error with a plain text message like "Invalid request".
  6. Look at the Question field. Both echo back the query, but Cloudflare includes a type field as an integer (e.g., 1 for A), while Google also uses an integer. The difference is that Google includes the name in the question exactly as sent (preserving case), while Cloudflare lowercases it. For example, querying Example.COM will echo Example.COM in Google's Question but example.com in Cloudflare's.
  7. Compare the AD (Authentic Data) flag handling. Both services include an AD flag in the flags array (Cloudflare) or as a boolean field ad (Google). Cloudflare includes ad: true in the flags array when DNSSEC is validated, while Google includes a separate ad field with a boolean value. This is a structural difference: Cloudflare puts it in the flags list, Google has a dedicated field.
  8. Check the CD (Checking Disabled) flag. Similarly, Cloudflare includes cd in the flags array, while Google has a separate cd field. This is consistent with the AD flag difference.
  9. Note the HTTP caching headers. While not part of the JSON body, the HTTP response headers differ. Cloudflare includes Cache-Control: max-age=300 based on the TTL, while Google includes Cache-Control: public, max-age=300 and also sets Date and Expires headers. This affects how you might cache responses on your side.
  10. Test with a non-existent domain. Query nonexistent.example.com to see the error format. Cloudflare returns Status: 3 (NXDOMAIN) with an empty Answer, while Google returns Status: 3 and a Comment field: "No such name". This is a clear difference in error reporting.

Common Mistakes

  • Not setting the Accept header. Both services require Accept: application/dns-json to return JSON; otherwise they return wire-format DNS (binary).
  • Assuming identical schemas. As shown, Cloudflare and Google have subtle differences in fields like flags vs. ad/cd and the presence of Comment in errors.
  • Ignoring the Status field. Always check Status before parsing Answer; a non-zero status means the answer may be empty or incomplete.
  • Misinterpreting TTL values. TTL is in seconds, not milliseconds. Some developers mistakenly treat it as milliseconds.

Tips

  • Use jq to pretty-print JSON for easier reading: curl ... | jq .
  • For automation, normalize the JSON by converting Cloudflare's flags array into booleans for ad and cd to match Google's format.
  • When caching responses, use the Cache-Control header from the HTTP response rather than the TTL in the JSON, as it may be adjusted by the provider.
  • Test with different record types (AAAA, MX, TXT) to see how the data field varies; for TXT records, the data is a string with quotes.

Troubleshooting

  • Getting a binary response instead of JSON: Ensure you set the Accept: application/dns-json header. If using a browser, you may need to use a tool like curl or a browser extension that sets custom headers.
  • HTTP 400 error from Cloudflare: This usually means the query parameters are malformed. Check that name is a valid domain and type is a valid DNS record type (e.g., A, AAAA, MX).
  • Google returns Status: 0 but empty Answer: This can happen if the query is valid but the record type is not supported (e.g., type=ANY). Google may return an empty answer with a Comment explaining the issue.
  • Fields missing in the response: If you don't see flags or ad, check the HTTP status code. Some errors return a minimal JSON with only Status and Comment.
  • TTL differences between providers: Cloudflare and Google may return different TTLs for the same record due to their caching policies. This is normal; the TTL is the time-to-live from the authoritative server, but the resolver may cap it.

Frequently Asked Questions

What is DNS-over-HTTPS (DoH)?

DoH is a protocol that sends DNS queries over HTTPS, encrypting the data to prevent eavesdropping and tampering. Both Cloudflare and Google offer public DoH resolvers that return JSON responses when requested.

Why do Cloudflare and Google have different JSON structures?

Each provider implements the DoH standard (RFC 8484) but adds their own extensions. Cloudflare includes a flags array, while Google uses separate boolean fields for AD and CD. Error handling also differs, with Google providing a Comment field.

Can I use the same code to parse both responses?

Yes, if you write code that checks for the presence of fields. For example, check if flags exists and parse it, otherwise look for ad and cd fields. The core fields (Status, Question, Answer) are identical.

What does the Status field mean?

It's the DNS response code. 0 means NOERROR (success), 3 means NXDOMAIN (domain doesn't exist), 2 means SERVFAIL, etc. Always check this before using the Answer.

How do I get the IP address from the Answer array?

Look for objects in the Answer array where type is 1 (A) or 28 (AAAA). The data field contains the IP address. For other types, data contains the record value (e.g., a hostname for MX).

Are these differences documented officially?

Yes. Cloudflare's documentation describes the flags field, and Google's documentation mentions the ad and cd fields. However, the exact behavior may change, so it's best to test with live queries.