How to extract data from a JSON field store in bigquery - google-bigquery

I have this JSON in a bigquery table
{"a":"b","superParams": {"isTest": "false"}}
And I would like to select superParas inner JSON
When I run this
select JSON_EXTRACT_SCALAR('{"a":"b",superParams":{"isTest":"false"}}','$.a')
I get
a
When I run this
select JSON_EXTRACT_SCALAR('{"a":"b",superParams":{"isTest":"false"}}','$.superParams')
I get
null
Instead of {"isTest":"false"}

The problem is that superParams is an object, not a scalar, so you need to use the JSON_EXTRACT function instead. Try this:
SELECT JSON_EXTRACT('{"a":"b","superParams":{"isTest":"false"}}','$.superParams')

In case if your intention was to actually extract value for example of isTest parameter - use below
SELECT JSON_EXTRACT_SCALAR('{"a":"b","superParams":{"isTest":"false"}}','$.superParams.isTest')

Related

Hive sql extract one to multiple values from key value pairs

I have a column that looks like:
[{"key_1":true,"key_2":true,"key_3":false},{"key_1":false,"key_2":false,"key_3":false},...]
There can be 1 to many items described by parameters in {} in the column.
I would like to extract values only of parameters described by key_1. Is there a function for that? I tried so far json related functions (json_tuple, get_json_object) but each time I received null.
Consider below json path.
WITH sample_data AS (
SELECT '[{"key_1":true,"key_2":true,"key_3":false},{"key_1":false,"key_2":false,"key_3":false}]' json
)
SELECT get_json_object(json, '$[*].key_1') AS key1_values FROM sample_data;
Query results

Big query unnest array with json values

Lets consider the following table on Google BigQuery:
WITH example AS (
SELECT 1 AS id, ["{\"id\":1, \"name\":\"AAA\"}", "{\"id\":2, \"name\":\"BBB\"}","{\"id\":3, \"name\":\"CCC\"}"]
UNION ALL
SELECT 2 AS id, ["{\"id\":5, \"name\":\"XXX\"}", "{\"id\":6, \"name\":\"ZZZ\"}"]
)
SELECT *
FROM example;
I would like to compose a query that will return names with their parent row's id.
like:
I tried using unnest with json functions and I just cant make this right.
Can anyone help me?
Thanks
Ido
According to your query, you already have json elements in your array. So with the use of unnest, you can use a json function like json_value to extract the name attribute of your elements.
select
id,
json_value(elt, '$.name')
from example, unnest(r) as elt;

How to access nested arrays and JSON in AWS Athena

I'm trying to process some data from s3 logs in Athena that has a complex type I cannot figure out how to work with.
I have a table with rows such as:
data
____
"[{\"k1\":\"value1\", \"key2\":\"value2\"...}]"
I'd like to treat it as (1) an array to extract the first element, and then that first element as the JSON that it is.
Everything is confused because the data naturally is a string, that contains an array, that contains json and I don't even know where to start
You can use the following combination of JSON commands:
SELECT
JSON_EXTRACT_SCALAR(
JSON_EXTRACT_SCALAR('"[{\"k1\":\"value1\", \"key2\":\"value2\"...}]"','$'),
'$[0].k1'
)
The inner JSON_EXTRACT_SCALAR will return the JSON ARRAY [{"k1":"value1", "key2":"value2"...}] and the outer will return the relevant value value1
Another similar option is to use CAST(JSON :
SELECT
JSON_EXTRACT_SCALAR(
CAST(JSON '"[{\"k1\":\"value1\", \"key2\":\"value2\"...}]"' as VARCHAR),
'$[0].k1'
)

How to query only a particular part of the string on BigQuery

I have a BigQuery table that has column called topics under which I have result like this
/finance/investing/funds/mutual funds. How do I write a query on BigQuery to yield me only the word between the first two slashes i.e. in this example I would like it to return only finance.
Just developing Gordons answer further to work with your ARRAY<STRING>. All you need to do, is just UNNEST the array before passing it to the SPLIT function mentioned before.
Simple sample:
SELECT SPLIT(string, '/')[safe_ordinal(2)]
FROM UNNEST([ '/finance/investing/funds/mutual funds', '/random/investing/funds/mutual
funds' ]) AS string
You can use split():
select split('/finance/investing/funds/mutual funds', '/')[safe_ordinal(2)]

BigQuery Standard SQL: how to return the first value of array?

Small working example
SELECT SPLIT("hello::hej::hallo::hoi", "::")
returns an array [hello, hej, hallo, hoi] where I want to select the first element i.e. hello. BG Standard provides no FIRST, instead FIRST_VALUE(..) OVER() which I cannot get working for this example above, so
How can I select the first value of array with BigQuery Standard SQL?
I think the documentation in BigQuery is pretty good. You can read about arrays here.
You can use either OFFSET() or ORDINAL(). The method would be:
select array[offset(0)]
or
select array[ordinal(1)]