I have a JSON array that looks similar to this
[{"key":"Email","slug":"customer-email","value":"abc#gmail.com"},{"key":"Phone Number","slug":"mobile-phone-number","value":"123456789"},{"key":"First Name","slug":"first-name","value":"abc"},{"key":"Last Name","slug":"last-name","value":"xyz"},{"key":"Date of birth","slug":"date-of-birth","value":"01/01/1990"}]
But the tricky part is, this array is stored as string. So I am thinking that the first step would be to convert the string into array then unnest it then follow the method in here
I wonder if this method is doable, if so I guess the challenge that I am having is to convert string into array. If not, or if you have more efficient method please help. Thanks
Have you tried json_extract_array
select json_extract_array(
"""[{"key":"Email","slug":"customer-email","value":"abc#gmail.com"},{"key":"Phone Number","slug":"mobile-phone-number","value":"123456789"},{"key":"First Name","slug":"first- name","value":"abc"},{"key":"Last Name","slug":"last-name","value":"xyz"},{"key":"Date of birth","slug":"date-of-birth","value":"01/01/1990"}]""");
Below is for BigQuery Standard SQL
#standardSQL
SELECT
id,
JSON_EXTRACT_ARRAY(json_string) AS json_array
FROM `project.dataset.table`
if to apply to sample data from your question as in below
#standardSQL
WITH `project.dataset.table` AS (
SELECT 1 id, '[{"key":"Email","slug":"customer-email","value":"abc#gmail.com"},{"key":"Phone Number","slug":"mobile-phone-number","value":"123456789"},{"key":"First Name","slug":"first-name","value":"abc"},{"key":"Last Name","slug":"last-name","value":"xyz"},{"key":"Date of birth","slug":"date-of-birth","value":"01/01/1990"}]' json_string
)
SELECT
id,
JSON_EXTRACT_ARRAY(json_string) AS json_array
FROM `project.dataset.table`
output is
Row id json_array
1 1 {"key":"Email","slug":"customer-email","value":"abc#gmail.com"}
{"key":"Phone Number","slug":"mobile-phone-number","value":"123456789"}
{"key":"First Name","slug":"first-name","value":"abc"}
{"key":"Last Name","slug":"last-name","value":"xyz"}
{"key":"Date of birth","slug":"date-of-birth","value":"01/01/1990"}
From this point - you can use solution in How do I parse value from JSON array into columns in BigQuery that you referenced in your question
Related
I wan to convert all the rows of BigQuery query output to an array of JSON.
For example: I want to convert the following output rows
Col1
Col2
ex1a
ex1b
ex2a
ex2b
Convert this to the following JSON:
{
"Col1":"ex1a",
"Col2":"ex1b"
},
{
"Col1":"ex2a",
"Col2":"ex2b"
}
]```
Use below approach
select format('[%s]', string_agg(to_json_string(t)))
from your_table t
if applied to sample data in your question - output is
Another option (with same output) is
select to_json_string(array_agg(t))
from your_table t
This is driving me nuts, and I don't understand what's wrong with my approach.
I generate a JSON object in SQL like this:
select #output = (
select distinct lngEmpNo, txtFullName
from tblSecret
for json path, root('result'), include_null_values
)
I get a result like this:
{"result":[{"lngEmpNo":696969,"txtFullName":"Clinton, Bill"}]}
ISJSON() confirms that it's valid JSON, and JSON_QUERY(#OUTPUT, '$.result') will return the array [] portion of the JSON object... cool!
BUT, I'm trying to use JSON_QUERY to extract a specific value:
This gets me a NULL value. Why??????? I've tried it with the [0], without the [0], and of course, txtFullName[0]
SELECT JSON_QUERY(#jsonResponse, '$.result[0].txtFullName');
I prefixed with strict, SELECT JSON_QUERY(#jsonResponse, 'strict $.result[0].txtFullName');, and it tells me this:
Msg 13607, Level 16, State 4, Line 29
JSON path is not properly formatted. Unexpected character 't' is found at
position 18.
What am I doing wrong? What is wrong with my structure?
JSON_QUERY will only extract an object or an array. You are trying to extract a single value so, you need to use JSON_VALUE. For example:
SELECT JSON_VALUE(#jsonResponse, '$.result[0].txtFullName');
I have a JSON object in this format data = {"1": {"col1":"a", "col2":"b"}, "2": {"col1":"c", "col2":"d"}....,"99":{"col1":"asd", "col2":"exm"}}. I would like to get all the values for col_1 and col_2 using athena. How do I achieve this using athena?
I was able to solve it by converting it to Map and then unnesting it like
select key, value from table where
unnest(cast(json_parse(data) as Map(varchar,JSON)) as t(key,value))
Ignore that it is a JSON format (since it is not simple enough) and use the regex function:
SELECT regexp_extract_all(column_data, '"col1":"([a-z]+)",'); -- [a, c, ... asd]
I have a big JSON data in one column called response_return in a Postgres DB, with a response like:
{
"customer_payment":{
"OrderId":"123456789",
"Customer":{
"Full_name":"Francis"
},
"Payment":{
"AuthorizationCode":"9874565",
"Recurrent":false,
"Authenticate":false,
...
}
}
}
I tried to use Postgres functions like -> ,->> ,#> or #> to walk through headers to achieve AuthorizationCode for a query.
When I use -> in customer_payment in a SELECT, returns all after them. If I try with OrderId, it's returned NULL.
The alternatives and sources:
Using The JSON Datatype In PostgreSQL
Operator ->
Allows you to select an element based on its name.
Allows you to select an element within an array based on its index.
Can be used sequentially: ::json->'elementL'->'subelementM'->…->'subsubsubelementN'.
Return type is json and the result cannot be used with functions and operators that require a string-based datatype. But the result can be used with operators and functions that require a json datatype.
Query for element of array in JSON column
This is not helpful because I don't want filter and do not believe that need to transform to array.
If you just want to get a single attribute, you can use:
select response_return -> 'customer_payment' -> 'Payment' ->> 'AuthorizationCode'
from the_table;
You need to use -> for the intermediate access to the keys (to keep the JSON type) and ->> for the last key to return the value as a string.
Alternatively you can provide the path to the element as an array and use #>>
select response_return #>> array['customer_payment', 'Payment', 'AuthorizationCode']
from the_table;
Online example
jSonColoumn - data type (TEXT)
sample rows
{"scheduledTime":"2014-01-29 19:55:00"}
{"scheduledTime":"2014-01-29 22:55:00"}
{"scheduledTime":"2014-01-29 15:55:00"}
{"scheduledTime":"2014-01-29 08:55:00"}
i need to sort this result with the "scheduledTime" key of the json object stored in the column "jSonColoumn". (Simply i need to order by the result based on the "scheduledTime" key of the json)
Thanks
There are some MySQL JSON functions.
Like this:
select json_extract(columnX, '{"scheduledTime"}')