Request a demo

BigQuery · JSON function

BigQuery TO_JSON_STRING

TO_JSON_STRING(value[, pretty_print]) serializes any SQL value, from a scalar to a struct, an array or a whole row, into JSON text. Use it to export rows as JSON, build API payloads, or compare and deduplicate entire rows.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

TO_JSON_STRING converts a SQL value into a JSON-formatted STRING. Pass a table alias (TO_JSON_STRING(t)) and you get the whole row as an object with one key per column, in column order. Structs become objects, arrays become JSON arrays, and SQL NULL becomes null, including a top-level NULL, which returns the four-character string null rather than SQL NULL.

Types are encoded for JSON consumers. DATE and TIME become strings, TIMESTAMP becomes ISO 8601 in UTC ("2026-11-23T10:00:00Z"), and BYTES becomes base64. INT64 values outside ±2^53 are written as strings so that parsers using doubles don't lose digits, and NUMERIC or BIGNUMERIC values with a fractional part are always quoted ("59.9").

Unlike TO_JSON, which returns the JSON type, TO_JSON_STRING keeps struct field order and duplicate field names, and its output is plain text that you can hash, compare or store in a STRING column. Set pretty_print to TRUE for indented, multi-line output.

Syntax

TO_JSON_STRING(value[, pretty_print])

Arguments and return value

ArgumentDescription
valueAny SQL value, including a table alias for the whole row.
pretty_printOptional BOOL. TRUE adds line breaks and indentation. NULL makes the result NULL.

Returns: A JSON-formatted STRING. The text null for a NULL value.

Examples

A whole row as JSON

WITH orders AS (
  SELECT 'A-100' AS order_id, DATE '2026-11-23' AS order_date, ['KB-1', 'MS-2'] AS skus, 2 AS qty
)
SELECT TO_JSON_STRING(o) AS order_json
FROM orders AS o;
Result
order_json
{"order_id":"A-100","order_date":"2026-11-23","skus":["KB-1","MS-2"],"qty":2}

How types are encoded

NUMERIC with decimals and INT64 above 2^53 are quoted; a whole NUMERIC and small integers are not.

SELECT TO_JSON_STRING(STRUCT(
  NUMERIC '59.90' AS amount,
  NUMERIC '120' AS whole_amount,
  9007199254740993 AS big_id,
  TIMESTAMP '2026-11-23 10:00:00' AS created_at,
  CAST(NULL AS STRING) AS coupon
)) AS payload;
Result
payload
{"amount":"59.9","whole_amount":120,"big_id":"9007199254740993","created_at":"2026-11-23T10:00:00Z","coupon":null}

Find duplicate rows

WITH snapshot AS (
  SELECT 1 AS id, 'active' AS status UNION ALL
  SELECT 1, 'active' UNION ALL
  SELECT 2, 'paused'
)
SELECT TO_JSON_STRING(s) AS row_json, COUNT(*) AS copies
FROM snapshot AS s
GROUP BY row_json
ORDER BY row_json;
Result
row_jsoncopies
{"id":1,"status":"active"}2
{"id":2,"status":"paused"}1

NULL, quotes, floats and bytes

SELECT
  TO_JSON_STRING(NULL) AS null_input,
  TO_JSON_STRING(NULL) IS NULL AS is_sql_null,
  TO_JSON_STRING('say "hi"') AS quoted_string,
  TO_JSON_STRING(1.0) AS float_one,
  TO_JSON_STRING(b'abc') AS bytes_value;
Result
null_inputis_sql_nullquoted_stringfloat_onebytes_value
nullfalse"say \"hi\""1"YWJj"

Common mistakes

  • Expecting numbers for NUMERIC columns. Values with decimals come out quoted; use TO_JSON, or cast to FLOAT64 first if the consumer needs JSON numbers and can accept float rounding.
  • Checking the result with IS NULL. TO_JSON_STRING(NULL) returns the text null, so the result is never SQL NULL.
  • Comparing JSON text built from differently ordered structs. Field order is part of the text, so {"a":1,"b":2} and {"b":2,"a":1} don't match.
  • Forgetting the time zone on timestamps. Output is always UTC with a Z suffix, whatever zone the data came from.

Cost and performance

  • TO_JSON_STRING(t) references every column of t, so on-demand billing counts all of them, as SELECT * would. Serialize a struct of the columns you need instead: TO_JSON_STRING(STRUCT(id, status)).

FAQ

How do I convert a row to JSON in BigQuery?

SELECT TO_JSON_STRING(t) FROM my_table AS t returns JSON text, and TO_JSON(t) returns a JSON value. For only some columns, pass a struct: TO_JSON_STRING(STRUCT(id, status)).

What is the difference between TO_JSON and TO_JSON_STRING?

TO_JSON returns the JSON type, which drops duplicate keys, doesn't keep key order and stores NUMERIC values as numbers. TO_JSON_STRING returns text that keeps field order and quotes NUMERIC values that have decimals.

Why are my NUMERIC values quoted in TO_JSON_STRING output?

TO_JSON_STRING writes NUMERIC and BIGNUMERIC values with a fractional part as strings so JSON parsers don't round them. Use TO_JSON, or cast to FLOAT64, if you need numbers.

More JSON functions

All BigQuery JSON functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.