I have a table with the following structure in bigquery,
id | desc |
---|---|
btjap | {"val":{"60fc-4955-8d9b":{"key":"top"},"4f96-4c9e-88f0":{"key":"left"}}} |
fxlol | {"val":{"c783-9342-h73s":{"key":"up"},"83hs-eudu-s839":{"key":"above"}, "37s9-dh3u-39sr":{"key":"right"}}} |
nretx | {"val":{}} |
The desc column contains multiple wildcard keys as value for the val key .
I want to unpack the data into the following table
id | code | key |
---|---|---|
btjap | 60fc-4955-8d9b | top |
btjap | 4f96-4c9e-88f0 | left |
fxlol | c783-9342-h73s | up |
fxlol | 83hs-eudu-s839 | above |
fxlol | 37s9-dh3u-39sr | right |
nretx |
any suggestions
2
Answers
This can be done using
json_extract_keys
to extract keys (code) from the json, then to access to the nested elements we can useREGEXP_EXTRACT
In case the function
json_extract_keys
is not found in your location then you can create an equivalent UDF :Result :
Similar to @SelVazi’s answer, but you can use UDFs as well and need to
LEFT JOIN
flattened arrays for your expected result.query results