I have a column uuid looking like as shown in the picture. It is a JSON type. I want to remove
the square brackets from each row and then the quotes (which I can remove using JSON_UNQUOTE). I tried using JSON_EXTRACT(uuid, '$[0]') but with this, I can only select one value at a time e.g. "5f5616fd88b3484bb636e6dbf5a702b6" not all the values inside the square brackets at once.
Once this is done, I want to remove quotes from each value and then again add brackets back. After this I want to export it as csv and use it for building a network graph using Networkx python library.
I am very open to suggestions, if my idea is wrong. Thank you!
You can't do that with JSON functions, because what you are trying to produce is not valid JSON.
However, you can process the json value with string functions. If you just want to replace the embedded double quotes, you can do:
replace(uuid, '"', '')
Related
I am processing a CSV file in powerautomate. Here is one record.
server2,usa,"rebooted,by citrix",25,good
Because "rebooted,by citrix" is data from a single field, when I am splitting with comma, the array manipulation gets mismatch.
I want to replace the comma within double quotes with hyphen. The expected output should be like server2,usa,"rebooted-by citrix",25,good
PrasadAthalye has a nice solution approach for this.
You could first split on the " character instead. Per item you could replace the , character and append the results to a new array. After that you should be able to apply your normal split.
https://powerusers.microsoft.com/t5/Building-Flows/Setting-up-specific-expression-to-remove-comma-inside-strings/m-p/646040/highlight/true#M86288
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+\":|"|,|}
i have a json field in my table(MySQL Database).
the following is the structure:
{
"article":{
"Key's 1":{
"value":"24"
}
"Key's of the something's 2":{
"value":"55"
}
}
}
i am trying to extract the "value" field of "Key's 1".
Due special character such as single quote and space, i am unable use JSON_EXTRACT function.
It give the error: Invalid JSON path expression.This error is around character position no : 10
My Query:
select
JSON_EXTRACT(analytics_json,'$.article.Key\'s 1.value')As value
from
tbl_json_data;
Even after place a backslash, i am getting the error.
You may escape each JSON path component in double quotes to handle special characters as well as spaces. The following works:
SELECT
JSON_EXTRACT(analytics_json,'$.article."Key''s 1".value') AS value
FROM tbl_json_data;
Demo
Note that your key name actually has two problems. First, it contains a literal single quote. We can handle that by just doubling up two single quotes. The key name also contains whitespace. By escaping in double quotes we may workaround this problem, but it would probably be best to avoid using JSON keys which have whitespace.
How to use locate function for string if string is present in database field?
I want to find location of "m/l" such string. below query return 0 to me however it is present in database table
SELECT locate('"m\/l":"',field) FROM table_name.
If you're dealing with escaped strings, you may need to crank up the escaping significantly to get it to work. Backslashes are special characters and will be collapsed down.
You may want to try:
LOCATE('"m\\/l":"',field)
LOCATE('"m\\\\/l":"',field)
If this is located inside another string, or two levels of string (e.g. inside JSON itself), you may need a lot of them.
I don't know if there is a better description of my problem, but here is what I need help with:
I have a field with lots of data, and the part I need to solve looks like this:
::field_x::<br />||field_x||519||/field_x||<br />||field_x||281||/field_x||<br />::/field_x::
I have to extract each number (id) from this, 519 and 281 in this example, and insert them in a field in another table, separated by spaces or commas. I know how to use SUBSTRING - LOCATE method, but that would return only the first instance, so is there a method to extract them all in one go?
SUBSTRING INDEX LOCATE will work. There is no built in functionality for regular expressions so unless you handle it before it gets to mysql...you're stuck using the SUBSTRING INDEX LOCATE method...
If you need to iterate through a dataset, you will need to initiate a cursor, or FOR loop and use a stored proc.
parse results in MySQL via REGEX