In Google BigQuery I have a column in a table which contains JSON objects similar to this one
select JSON '[{"id":"A","value":"1"},{"id":"B","value":"2"},{"id":"C","value":"John"},{"id":"D","value":null},{"id":"E","value":"random"}]' as JSON_
I want to create a new column with the value of the JSON object where the id is equal to "B".
One way to solve this is as follows. But it only works if it is always the second object which has id = "B".
With table_ as (
select JSON '[{"id":"A","value":"1"},{"id":"B","value":"2"},{"id":"C","value":"John"},{"id":"D","value":null},{"id":"E","value":"random"}]' as JSON_
)
select JSON_EXTRACT(JSON_, '$[1].value') as value_2
from table_
Is there a more reliable method how to extract the json object value according to the specific id?
Thank you for your time and help!
2
Answers
bigquery is great for handle jsons and unstructured data, but sometimes its a pain to find the correct way
in your case, we need to:
the query is the following
You can consider below if you’re interested only in an id
B
.