Home > Enterprise >  How do I extract all JSON values from all keys in SQL Bigquery?
How do I extract all JSON values from all keys in SQL Bigquery?

Time:01-22

I have a json string in bigquery that looks like that:

{"1":"eggs","2":"nuts","3":"fish"}

How could I extract all values without listing the keys? What I need is:

['eggs', 'nuts', 'fish']

I've tried [json_extract(json_string, '$[1]'), json_extract(json_string, '$[2]')] and it does the job but it won't work if the number of keys increases

CodePudding user response:

One way to extract all the values from a JSON string without listing the keys, is to use the json_extract_scalar function in combination with a subquery that generates all the key paths for the JSON string.

WITH data AS (
  SELECT '{"1":"eggs","2":"nuts","3":"fish"}' AS json_string
),
keys AS (
  SELECT 
    CONCAT('$.',to_json_string(i)) as key_path
  FROM 
    UNNEST(GENERATE_ARRAY(1, (SELECT MAX(i) FROM UNNEST(json_keys(json_string))))) i
)
SELECT 
  json_extract_scalar(json_string, key_path) as value
FROM 
  data, keys

This query will return all the values from the JSON string as a single column in a table. Note that this solution would work even if the number of keys increases. You can use the ARRAY_AGG if you need to get the result as an array.

CodePudding user response:

I apologize, I made an error in my previous response. json_keys() function is not a built-in function in BigQuery, it's a function available in other SQL engines.

One way to extract all the values from a JSON string without listing the keys is to use the json_extract() function in combination with a subquery that generates all the key paths for the JSON string, like this:

WITH data AS (
  SELECT '{"1":"eggs","2":"nuts","3":"fish"}' AS json_string
)
SELECT 
  json_extract(json_string, to_json_string(i)) as value
FROM 
  data, UNNEST(GENERATE_ARRAY(1, (SELECT MAX(i) FROM UNNEST(REGEXP_EXTRACT_ALL(json_string, r'\"([^\"] )\"'))))) i

This query will return all the values from the JSON string as a single column in a table, ['eggs', 'nuts', 'fish'] You can use the ARRAY_AGG to get the result as an array.

Please note that this solution will work only if your JSON string is in the format of: {"key1": "value1", "key2": "value2",...} and it will not work if the key is not a number

  • Related