Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
How to return a MAP like structure from bigquery javascript UDF so that I can generate a key-value column dynamically at runtime?

I'm trying to return a MAP like structure from Javascript UDF in bigquery. So that I can convert that structure directly into relational columns without knowing the column named prior. In below approach, I'm trying to stringify JSON and then using json_extract_scaler function to create columns.

CREATE TEMP FUNCTION main(json_str STRING)
RETURNS STRING
LANGUAGE js AS 
r"""
var row = JSON.parse(json_str);
return JSON.stringify(row);
""";

with temp_table as (
  select "ram" name, "ram@gmail.com" email
),
Rule_result as (SELECT main(TO_JSON_STRING(STRUCT(t.name, t.email))) result FROM temp_table as t)
SELECT json_extract_scalar(result, '$.name') name,  
json_extract_scalar(result, '$.email') email
FROM Rule_result as r;

In this approach, I'm returning the struct, knowing the column names beforehand.

CREATE TEMP FUNCTION main(json_str STRING)
RETURNS STRUCT<name STRING, email STRING>
LANGUAGE js AS 
r"""
var row = JSON.parse(json_str);
return row;
""";

with temp_table as (
  select "ram" name, "ram@gmail.com" email
),
Rule_result as (SELECT main(TO_JSON_STRING(STRUCT(t.name, t.email))) result FROM temp_table as t)
SELECT r.result.* FROM Rule_result as r;

Both the approaches work fine. But it doesn't solve the problem. Because I need to be aware of the column names. Bigquery supports struct return type but that doesn't fit my usecase. Since I'm not aware about the column names beforehand. How can I dynamically create columns from the data return by the javascript UDF without knowing the column names?

  • data is flat JSON object

{
  "name":"ram",
  "email":"ram@gmail.com"
}

I somehow need to convert this JSON object into table columns like

name email
ram ram@gmail.com
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Consider below

create temp function  extract_keys(input string) returns array<string> language js as """
  return Object.keys(JSON.parse(input));
""";
create temp function  extract_values(input string) returns array<string> language js as """
  return Object.values(JSON.parse(input));
""";
create temp table tmp as 
select id, key, value 
from your_table,
unnest(extract_keys(json)) key with offset
join unnest(extract_values(json)) value with offset
using(offset);

execute immediate (select
'''select * from tmp
pivot (any_value(value) for key in (''' || string_agg(distinct "'" || key || "'") || '''))
'''
from tmp
);    

if applied to sample data like in your question's example

enter image description here

output is

enter image description here

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!