Request a demo

BigQuery · JSON function

BigQuery TO_JSON

TO_JSON(value) converts a SQL value (scalar, struct, array or whole row) into a value of the JSON type. Use it to build JSON columns and nested payloads you can keep querying with dot access; use TO_JSON_STRING when you need text.

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

TO_JSON produces the JSON type, so the result can be stored in a JSON column, passed to JSON functions or read back with dot access (TO_JSON(t).items[0].sku). Structs become objects, arrays become JSON arrays, DATE and TIMESTAMP become strings, and SQL NULL becomes JSON null.

Because the result is a JSON value, it follows the JSON type's rules: object keys are not kept in struct order (the console shows them sorted), and when a struct has duplicate field names only the first one is kept. NUMERIC and BIGNUMERIC values are written as JSON numbers.

Large integers are the one setting to decide on. By default an INT64 such as 9007199254740993 is stored as a JSON number. Consumers that parse numbers as doubles, such as JavaScript, lose precision above 2^53, and stringify_wide_numbers => TRUE writes those values as strings instead.

Syntax

TO_JSON(sql_value[, stringify_wide_numbers => { TRUE | FALSE }])

Arguments and return value

ArgumentDescription
sql_valueAny SQL value, including a table alias for the whole row.
stringify_wide_numbersOptional named BOOL, default FALSE. TRUE writes INT64, NUMERIC and BIGNUMERIC values outside the FLOAT64-safe range as JSON strings.

Returns: JSON.

Examples

TO_JSON and TO_JSON_STRING on the same row

The JSON value lists keys in sorted order; the string keeps column order.

WITH orders AS (
  SELECT 'A-100' AS order_id, 59.9 AS total, ['KB-1', 'MS-2'] AS skus
)
SELECT TO_JSON(o) AS order_json, TO_JSON_STRING(o) AS order_text
FROM orders AS o;
Result
order_jsonorder_text
{"order_id":"A-100","skus":["KB-1","MS-2"],"total":59.9}{"order_id":"A-100","total":59.9,"skus":["KB-1","MS-2"]}

Build a nested payload per order

WITH order_lines AS (
  SELECT 'A-100' AS order_id, 'KB-1' AS sku, 1 AS qty UNION ALL
  SELECT 'A-100', 'MS-2', 2
)
SELECT TO_JSON(STRUCT(
  order_id,
  ARRAY_AGG(STRUCT(sku, qty) ORDER BY sku) AS items
)) AS payload
FROM order_lines
GROUP BY order_id;
Result
payload
{"items":[{"qty":1,"sku":"KB-1"},{"qty":2,"sku":"MS-2"}],"order_id":"A-100"}

Large integers and other types

SELECT
  TO_JSON(9007199254740993) AS default_mode,
  TO_JSON(9007199254740993, stringify_wide_numbers => TRUE) AS stringified,
  TO_JSON(NUMERIC '59.90') AS numeric_value,
  TO_JSON(DATE '2026-11-23') AS date_value;
Result
default_modestringifiednumeric_valuedate_value
9007199254740993"9007199254740993"59.9"2026-11-23"

Query the result directly

A SQL NULL becomes a JSON null, which JSON_TYPE reports as 'null'.

SELECT
  TO_JSON(STRUCT('A-100' AS order_id, [STRUCT('KB-1' AS sku)] AS items)).items[0].sku AS first_sku,
  JSON_TYPE(TO_JSON(CAST(NULL AS STRING))) AS null_type;
Result
first_skunull_type
"KB-1"null

Common mistakes

  • Expecting key order to match the struct. JSON values don't keep object member order; use TO_JSON_STRING when order matters.
  • Relying on duplicate field names. TO_JSON keeps only the first: TO_JSON(STRUCT(1 AS id, 2 AS id)) is {"id":1}.
  • Sending large IDs to JavaScript as numbers. Values above 2^53 lose precision there; set stringify_wide_numbers => TRUE or cast the IDs to STRING.
  • Grouping or comparing TO_JSON results. JSON values can't be grouped or compared; use TO_JSON_STRING for that.

FAQ

What is the difference between TO_JSON and TO_JSON_STRING?

TO_JSON returns the JSON type, which you can query with dot access and store in JSON columns. TO_JSON_STRING returns text that keeps field order and can be grouped, compared or hashed.

How do I create a JSON column from existing columns?

SELECT TO_JSON(STRUCT(col_a, col_b)) AS payload creates a JSON value per row, and writing it to a table creates a JSON column. For key-value pairs, JSON_OBJECT is an alternative.

How do I convert a whole row to JSON?

SELECT TO_JSON(t) FROM my_table AS t. Every column becomes a key of the object.

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.