1

I referred GCP Big query documentation as shown below link but there is no luck achieving the objective about using these functions to retrieve the non-null valued JSON data type column values from a Big Query Table.

Ref help: https://cloud.google.com/bigquery/docs/reference/standard-sql/json-data#json_nulls

Example Select Query: Select column1 from EMP where column1 != 'null'

1
  • Can you provide sample data and your expected sample output for proper replication? Commented Oct 13, 2022 at 6:38

2 Answers 2

4

When you try to compare JSON type with other type, you'll get

SELECT JSON "null" != 'null'

No matching signature for operator != for argument types: JSON, STRING. Supported signature: ANY != ANY at [1:8]

Simple workaround for this is to convert JSON data to BigQuery String and compare it with others.

WITH json_data AS (
  SELECT CAST(null AS JSON) AS col1 UNION ALL
  SELECT JSON 'null' UNION ALL
  SELECT JSON '{"a":null}' UNION ALL
  SELECT JSON '{"b":"hello"}'
)
SELECT *, FORMAT('%t', col1) format, JSON_TYPE(col1) json_type,
  FROM json_data;

enter image description here

FORMAT() and JSON_TYPE() function convert JSON 'null' to BigQuery string respectively. Utilizing one of these output, you can retrieve the non-null valued JSON data type column values

SELECT * FROM json_data WHERE JSON_TYPE(col1) != 'null';

or,

SELECT * FROM json_data WHERE LOWER(FORMAT('%t', col1)) != 'null';

Note FORMAT() turns null into NULL string.

Sign up to request clarification or add additional context in comments.

Comments

1

Use JSON_TYPE assuming you want to find all values that are not explicit JSON NULLs?

Select column1 from EMP where JSON_TYPE(column1) != 'null'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.