Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse JSON in Standard SQL BigQuery?

After streaming some json data into BQ, we have a record that looks like:

"{\"Type\": \"Some_type\", \"Identification\": {\"Name\": \"First Last\"}}"

How would I extract the type from this? E.g. I would like to get Some_type.

I tried all possible combinations shown in https://cloud.google.com/bigquery/docs/reference/standard-sql/json_functions without success, namely, I thought:

SELECT JSON_EXTRACT_SCALAR(raw_json , "$[\"Type\"]") as parsed_type FROM `table` LIMIT 1000 

is what I need. However, I get:

Invalid token in JSONPath at: ["Type"]

Picture of rows preview

enter image description here

like image 946
GRS Avatar asked Mar 05 '23 11:03

GRS


1 Answers

Below example is for BigQuery Standard SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 id, "{\"Type\": \"Some_type\", \"Identification\": {\"Name\": \"First Last\"}}" raw_json UNION ALL
  SELECT 2, '{"Type": "Some_type", "Identification": {"Name": "First Last"}}'
)
SELECT id, JSON_EXTRACT_SCALAR(raw_json , "$.Type") AS parsed_type
FROM `project.dataset.table`  

with result

Row id  parsed_type  
1   1   Some_type    
2   2   Some_type    

See below update example - take a look at third record which I think mimic your case

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 id, "{\"Type\": \"Some_type\", \"Identification\": {\"Name\": \"First Last\"}}" raw_json UNION ALL
  SELECT 2, '''{"Type": "Some_type", "Identification": {"Name": "First Last"}}''' UNION ALL
  SELECT 3, '''"{\"Type\": \"


                     null1\"}"
  '''
)
SELECT id, 
  JSON_EXTRACT_SCALAR(REGEXP_REPLACE(raw_json, r'^"|"$', '') , "$.Type") AS parsed_type
FROM `project.dataset.table`  

with result

Row id  parsed_type  
1   1   Some_type    
2   2   Some_type    
3   3   null1    

Note: I use null1 instead of null so you can easily see that it is not a NULL but rather string null1

like image 87
Mikhail Berlyant Avatar answered Mar 27 '23 23:03

Mikhail Berlyant