I have the following value as String in one column in bigquery table. I need to extract the content value using BigQuery view.
{type:System.Int32,content:202104}
I have converted the string like below and tried the JSON_EXTRACT syntax its working.
SELECT
JSON_EXTRACT(JSON'{"type":"System.Int32","content":202104}', '$.content')
AS json_data;
But I have converted this manually from
{type:System.Int32,content:202104}
to
{"type":"System.Int32","content":202104}
How can I achieve the same using query? Can anyone help me to resolve this query, Thanks in advance!
You may use REGEXP_EXTRACT to directly extract the value of the "content". Please refer to BigQuery REGEXP_EXTRACT for more information on the usage of this string function.
SELECT REGEXP_EXTRACT('{type:System.Int32,content:202104}', r'content:(\d+)') as CONTENT_EXTRACT
Output:
This may help:
SELECT
JSON_EXTRACT_SCALAR(REGEXP_REPLACE('{type:System.Int32,content:202104}', r'[^\:\,{}]+', r'"\0"'), '$.content')
AS json_data
Related
I want to extract the new status value from the below JSON
{"old_status":"Testing(PhaseI)","new_status":"Production(PhaseII)","user":"Developer","reason":null}
so the output required is Production(PhaseII), pls note that the new_status's values are completely dynamic , how to do this using JSON_EXTRACT()
SELECT JSON_UNQUOTE(JSON_EXTRACT(`field``, '$.new_status'))
I'm extracting 2 fields from a JSON using JSON_EXTRACT using BQ as the following:
select JSON_EXTRACT_SCALAR('Event_Value','$.user_id') as cid, JSON_EXTRACT_SCALAR('Event_Value','$.tsts') as ts
if the JSON format is missing one of the field I'm receiving NULLs all over the place.
Is there a way to overcome it?
I feel the fix is quite simple:
select JSON_EXTRACT_SCALAR(Event_Value,'$.user_id') as cid, JSON_EXTRACT_SCALAR(Event_Value,'$.tsts') as ts
So, there are extra ' around Event_Value - thus Event_Value was treated not as a column name but rather as string
I am using MySQL 5.7+ with the native JSON data type.
Sample data:
set #jsn_string='{\"body\": {\"items\": \"[{\\\"count\\\":530,\\\"id\\\":5},{\\\"count\\\":1,\\\"id\\\":519},{\\\"count\\\":209,\\\"id\\\":522},{\\\"count\\\":0,\\\"id\\\":3004}] \"}}';
Questions: the correct answer is 530
The following query has the position of the data
select json_extract(#jsn_string,'$.body.items[0].id[0]');
but the result is : null
we can use json_unquote to remove those double quotes in items[0]
set #jsn_string='{\"body\": {\"items\": \"[{\\\"count\\\":530,\\\"id\\\":5},{\\\"count\\\":1,\\\"id\\\":519},{\\\"count\\\":209,\\\"id\\\":522},{\\\"count\\\":0,\\\"id\\\":3004}] \"}}';
select json_extract(json_unquote(json_unquote(json_extract(#jsn_string, '$.body.items[0]')))
,'$[0].count');
see dbfiddle
As per your sample JSON, id is not an array. So id[0] wont work As I understand from your question, you want to retrieve 530. For this you can use the following.
select json_extract(#jsn_string,'$.body.items[0].count');
Please let me know if I understood it incorrectly.
Edit - 1:
I think your json is invalid. Array should not be wrapped in braces. Try this
#jsn_string='{\"body\": {\"items\": [{\\\"count\\\":530,\\\"id\\\":5},{\\\"count\\\":1,\\\"id\\\":519},{\\\"count\\\":209,\\\"id\\\":522},{\\\"count\\\":0,\\\"id\\\":3004}]}}';
I am trying to run mysql query to get the part of string below is string stored in database
nb=5&pfid=2098&rssurl=http%3A%2F%2Ffeeds.feedburner.com%2Ffoxnews%2Flatest&nb=5
using mysql query i need to extract the pfid which is '2098' and insert it into other table field. i tried few queries but i am not able to achieve it.
anybody have idea how to do this may be mysql expert...or sql query expert.
thank you in advance
regards,
mona
You can do this with instr and substring:
select left(val, instr(val, '&')-1)
from (
select substring(col,instr(col, 'pfid=')+5,length(col)) val
from yourtable
) t
SQL Fiddle Demo
This does assume the string value will contain an & after the value. If not, you could include a little additional logic to check.
In regards to inserting that value into another field, should be fairly straight-forward now that you have the value. Depends on your exact needs.
One column returns such values:
Something";s:5:"value";s:3:"900";s:11:"print_
I want to extract all numbers that are at least 3 digits long, in the above case thats 900. How can I do that in MySQL? Maybe using a regex? I cant use any index, the length of the string and the number in the string can be different.
Thanks!
Try unserialize() it if you are using PHP! And then var_dump it to see the strings and arrays
You can't extract them using MySQL, use any other language for that.
What you can do is include a Where Clause, that will make the work easier for your script.
Assuming your column is called "serialized" in the table "example"
SELECT serialized FROM example WHERE serialized REGEXP '[0-9]{3,}'
Please note that REGEXP is just outputting 1 or 0
After you did the query, use the regex functions of your language do extract the numbers like so:
([0-9]{3,})*