I Currently have a column in json format with multiple items.
The struct is like the following:
phones": [{"phone": "11111111", "type": "CELLPHONE"}, {"phone":
"222222222", "type":"CELLPHONE"}, {"phone": "99999999", "type":
"CELLPHONE"}]
I tried:
json_extract(Contacts,'$.phones.phone') as phone_number but it only extracts the first one.
JSON_EXTRACT_ARRAY(Contacts,'$.phones') as phone_contacts gives me an array, But Im getting error trying to unnest it.
Does anybody know any approach to solve this problems?
Thanks in Advance.
Consider below
select json_value(phone_contact, '$.phone') phone,
json_value(phone_contact, '$.type') type
from your_table,
unnest(json_extract_array(Contacts,'$.phones')) phone_contact
if applied to sample data in your question - output is
Related
I got a problem with a specific database where some tags from a user profile are stored as a String data type, and so far I have not figured out how to fetch data from this one.
Example String:
{"watch_intro": "Gifts from my family.", "favorite_brands_styles": ["Fossil","Tudor","Omega"]}
I am looking to query into this field and get something like below:
'Fossil, Tudor, Omega'
Any help would be appreciated, thanks.
Nevermind, found the answer:
array_to_string(PARSE_JSON(EXTRA_INFO):"favorite_brands_styles",', ') AS fav_brands
I have a table. There are many columns and rows. One column that I am trying to query in Snowflake has semi structured data. For example, when I query
select response
from table
limit 5
This is what is returned
[body={\n "id": "xxxxx",\n "object": "charge",\n "amount": 500,\n "amount_refunded": 0,\n "application": null,\n "application_fee": null,\n "application_fee_amount": null,\n "balance_transaction": null,\n "billing_details": {\n "address": {\n "city": null,\n "zip": "xxxxx",]
I want to select only the zip in this data. When I run code:
select response:zip
from table
limit 5
I get an error.
SQL compilation error: error line 1 at position 21 Invalid argument types for function 'GET': (VARCHAR(16777216), VARCHAR(11))
Is there a reason why this is happening? I am new to snowflake so trying to parse out this data but stuck. Thanks!
Snowflake has very good documentation on the subject
For your specific case, have you attempted to use dot notation? It's the appropiate method for accessing JSON. So
Select result:body.zip
from table
Remember that you have your 'body' element. You need to access that one first with semicolon because it's a level 1 element. Zip is located within body so it's a level 2. Level 1 elements are accessed with semicolon, level 2 elements are accessed with dot notation.
I think you have multiple issues with this.
First I think your response column is not a variant column. Please run the below query and confirm
SHOW COLUMNS ON table;
Even if the column is variant, the way the data is stored is not in a valid JSON format. You will need to strip the JSON part and then store that in the variant column.
Please do the first part and share the information, I will then suggest next steps. I wanted to put that in the comment but comment does not allow to write so many sentences.
let say that the json object is > { "foo": "bar"}
after stringyfing i got > "{ \"foo\": \"bar\" }"
how can I get back the orginal json object using UPDATE sql query?
i'm aware of that it's a bad DB architecture it was designed by another engineer before me, that's why I would like get back the original json data and then alter the column to jsonb
Update:
please be aware that I'm looking for an answer to do that with only sql query and without any involving of programming languages like javascript.. etc
I was able to solve this same issue by doing some regexp replaces. You might not need the where clause, but in my case, I had a bug that started to stringify the JSONB column so only some of my data needed this change applied.
update your_table
set text =
jsonb(regexp_replace(regexp_replace(regexp_replace(text::text, '"$', ''), '^"+', ''), '\\"', '"', 'g'))
where text->>'foo' is null;
You could do to_json('{ \"foo\": \"bar\" }'::text)
so it would be something like
update yourtable set yourjsoncolumn = to_json(yourjsoncolum::text)
I tried to load logs from Google Cloud Storage to BigQuery by the bq command
and I've got this error "Could not convert value to string".
my example data
{"ids":"1234,5678"}
{"ids":1234}
my example schema
[
{ "name":"ids", "type":"string" }
]
It seems IDs can't convert by none quote at single ID.
Data is made with fluent-plugin-s3, but more than one ID connected by a comma can be bound up with a quotation and isn't made single id.
How can I load these data to BigQuery?
Thanks in advance
Well check different fluentd plugins that can help you, maybe
https://github.com/lob/fluent-plugin-json-transform
https://github.com/tarom/fluent-plugin-typecast
I'm a newbie to rails.I have a model called OfflineExport. In the table I have data like this
#<OfflineExport id: 2,
parameters:
{"project_id"=>"3",
"type"=>"submissions",
"filters"=>{"task_type"=>"",
"corrections"=>"", "grade"=>"",
"min_duration"=>"", "after"=>"",
"max_duration"=>"", "reviews"=>"",
"before"=>""},
"send_email"=>"true",
"options"=>{"offline_record_id"=>2}}>
Am trying to fetch parameters["project_id"] in where clause like
OfflineExport.where("parameters[project_id] = '3'")
But am getting error like:
ActiveRecord::StatementInvalid: PGError: ERROR: cannot subscript type text because it is not an array
can anyone help me in solving this?
It seems that you have a serialized column in your model. Something like this: serialize :parameters. This means the data is stored in the database not in an easily readable format, which leads that you can not query on it. Same as here.
Solution: extract the field you want to query on, and make a column for it.