简体   繁体   中英

Get a structured value from string field on BigQuery

I have a string field on BigQuery which looks like this:

[{"value": "yes", "source": "CURATED", "curated": true}, {"value": "yes", "source": "listing_service", "curated": false}]

How can I query this field if I only like to know the value parameter which in this case is yes ?

Consider below approach

select 
  json_value(entry, '$.value') as value,
  json_value(entry, '$.source') as source,
  json_value(entry, '$.curated') as curated
from your_table, unnest(json_extract_array(field_name)) entry      

if applied to sample data in your question - output is

在此处输入图像描述

Meanwhile I found the solution for this with the function JSON_EXTRACT_SCALAR The exact example could be queried like this.

JSON_EXTRACT_SCALAR(field_name,"$[0].value")

To complement your answer, JSON_Extract_SCALAR is a good way to extract data but its consider legacy as show on JSON functions documentation page. I would suggest to use JSON_VALUE instead.

select JSON_VALUE('[{"name":"juliana","country":"peru"}]',"$[0].name") as name

output

name 
-------
juliana

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM