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
Related
Need help
I have a PostgreSQL Function, which will update a JSON Column having data like "{"abcd:"xyz"}"
with a similar format and return the output.
I am using CTE Query with Returning clause. Only problem is its giving output as Double Escaped i.e "{\""abcd:\""xyz""}".
And if I am using (columnName #>> '{}' )::json, its giving me back "{""abcd:""xyz""}".
How do I get correct output from the function.
Select Query on the table gives as it is , but output from function gives Double-Escaped. How do I solve it.
Any suggestion please.
I have one column(varchar) containing only json string within one table. I want replace all keys with "" on that column. How can I do that using sql? My database is MySQL.
For example:
|--------------------------------------------------------------------|
| t_column |
|--------------------------------------------------------------------|
| {"name":"mike","email":"xxx#example.com","isManage":false,"age":22}|
|--------------------------------------------------------------------|
SELECT replace(t_column, regexp, "") FROM t_table
I expect:
mikexxx#example.comfalse22
How to write that regexp?
Start from
select t_column->'$.*' from test
This will return a JSON array of attribute values:
[22, "mike", "xxx#example.com", false]
This might be already all you need, and you can try something like
select *
from test
where t_column->'$.*' like '%mike%';
Unfortunately there seems to be no native way to join array values to a single string like JSON_ARRAY_CONCAT(). In MySQL 8.0 you can try REGEXP_REPLACE() and strip all JSON characters:
select regexp_replace(t_column->'$.*', '[" ,\\[\\]]', '') from test
which will return '22mikexxx#example.comfalse'.
If the values can contain one of those characters, they will also be removed.
Note: That isn't very reliable. But it's all I can do in a "simple" way.
See demo on db-fiddle.
I could be making it too simplistic, but this is just a mockup based on your comment. I can formalize it into a query if it fits your requirement.
Let's say you get your JSON string to this format where you replace all the double quotes and curly brackets and then add a comma at the end. After playing with replace and concat_ws, you are now left with:
name:mike,email:xxx#example.com,isManage:false,age:22,
With this format, every value is now preceded by a semicolon and followed by a comma, which is not true for the key. Let's say you now want to see if this JSON string has the value "mike" in it. This, you could achieve using
select * from your_table where json_col like '%:mike,%';
If you really want to solve the problem with your approach then the question becomes
What is the regex that selects all the undesired text from the string {"name":"mike","email":"xxx#example.com","isManage":false,"age":22} ?
Then the answer would be: {\"name\":\"|\"email\":\"|\",\"isManage\":|,\"age\":|}
But as others let you notice I would actually approach the problem parsing JSONs. Look up for functions json_value and json_query
Hope I helped
PS: Keep close attention on how I structured the bolded sentence. Any difference changes the problem.
EDIT:
If you want a more generic expression, something like select all the text that is not a value on a json-formatted string, you can use this one:
{|",|"\w+\":|"|,|}
The fields looks like this:
[{"Tester":"Bonnie","Credentials":"MS","Date":"2013-02-19"},
{"Tester":"Karen","Credentials":"Teacher","Date":"2016-01-20"}]
I need to select based on the year of the 'Date' value: < > or =
I have found the json_extract function and it's shortcuts such that this will get the data (from the MySQL docs) "autowrapped as an array" and it works:
json_field->"$[*].Date" returns ["2013-02-19", "2016-01-20"]
Great, so I has the dates from the json data, but now I need to format the WHERE. How do I select the record if either of the years is 2016 for instance? I don't see any json function that do that.
I would highly recommend you to drop this JSON column and build another table, then you can search in your table using JOIN. JSON in column is a bad idea.
Just to answer your question:
I think this should work -
WHERE YEAR(STR_TO_DATE(json_field->"$.Date", '%Y-%m-%d')) = '2016'
I have a json data like this
{"0":"6","1":"5","2":"10"}
And on the DB I have table which contains json datas like these
{"0":"6","1":"4"}
{"0":"5","1":"2","2":"7"}
{"0":"3","1":"10","2":"4"}
{"0":"6","1":"5","2":"10","3":"8"}
So, I would like know is it possible or does it make sense to select data by comparing the json datas?
I would like to get any json that may contain any key:value in my input json.
So, from my example they will be these
{"0":"6","1":"4"}
{"0":"6","1":"5","2":"10","3":"8"}
You can use JSON search functions. For example -
SELECT json_field FROM table1
WHERE
JSON_CONTAINS(json_field, '{"0":"6"}')
AND JSON_CONTAINS(json_field, '{"1":"5"}')
AND JSON_CONTAINS(json_field, '{"2":"10"}');
Good Day,
I am trying to get a point from an SQL column. CUrrently it is encoded as a string of characters: Gþ`C#Ëóàî¬Ë]À
So is there any way to actually decode it to get the actual value?
Use MySQL's AsText() function:
SELECT AsText(myPoint) FROM myTable