Skip to content
Advertisement

fetch the data from array of objects sql BigQuery

I need to fetch key value pairs from the second object in array. Also, need to create new columns with the fetched data. I am only interested in the second object, some arrays have 3 objects, some have 4 etc. The data looks like this:

The desired output:

Advertisement

Answer

Below is for BigQuery Standard SQL

if applied to sample data from your question – output is

enter image description here

as you can see, the “trick” here is to use proper regexp in regexp_replace function. I’ve included now any alphabetical chars and - . you can include more as you see needed As an alternative yo can try regexp_replace(mapping, r"(: )([^,}]+)", "\1'\2'") as in below example – so you will cover potentially more cases without changes in code

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement