Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to extract the productType variable from a json file in big query

XZ
New Member

JSON format as follows:

"event_data_inputs": [
  {  "name": "assessmentrequest.session.1.identifierkey",
     "value": "ProductType" },
  { "name": "rulesresponse.assessmentrequest.session.1.identifiervalue",
    "value": "PAID" }, 
....
  ]

 
I tried the following query but failed: 
 
select distinct time as dt, referenceid
(select ds.value from unnest(event_data_inputs) ds where ds.name =( select concat('rulesresponse.',left(r.name, length(r.name)-3),'value') from unnest(event_data_inputs)  r where UPPER(r.value) =UPPER("ProductType") )) as ProductType
 
from aaa

 

0 1 627
1 REPLY 1

To accurately extract the "productType" variable from your JSON structure in BigQuery, you should use a query that handles the nested array event_data_inputs. The following SQL query is designed for this purpose:

SELECT
 DISTINCT time AS dt,
 referenceid,
 (
  SELECT value
  FROM UNNEST(event_data_inputs)
  WHERE name = 'assessmentrequest.session.1.identifierkey'
   AND UPPER(value) = UPPER('ProductType')
 ) AS ProductType
FROM 
 aaa;