How can I use JSON_EXTRACT in MySQL and get a string without the quotes?
You can use JSON_UNQUOTE
to achieve this.
select JSON_UNQUOTE(JSON_EXTRACT(base, '$.scope')) as scope from t_name
ref: Functions That Modify JSON Values
MySQL: json extract remove quotes
I guess that the error is related to the connector driver. I'm using mysql-connector-java-5.1.45 with SQL Developer. Still haven't found a resolution (tryed also another version) but doing the replace directly on the mysql server it works.
Extract value by key with double quote and backslash in json column
Here's a test:
mysql> select json_extract(cast('{"simple": "val1", "h\\"simple": "val2"}' as json),
'$."h\\"simple"') as h_simple;
+----------+
| h_simple |
+----------+
| "val2" |
+----------+
You need a literal \
in the JSON path expression so it escapes the "
to JSON. But MySQL strings treat backslash as special, so you have to use a double-backslash to ensure that a single literal backslash makes it past MySQL's string literal parser.
Update and replace unquoted JSON strings
You may use the next procedure:
CREATE PROCEDURE quote_value(max_amount INT)
BEGIN
REPEAT
UPDATE test
SET settings = JSON_REPLACE(settings, CONCAT('$[', max_amount, '].value'), CAST(JSON_UNQUOTE(JSON_EXTRACT(settings, CONCAT('$[', max_amount, '].value'))) AS CHAR));
SET max_amount = max_amount - 1;
UNTIL max_amount < 0 END REPEAT;
END
max_amount
parameter defines the amount of objects in the array to be updated (do not forget that the array elements are counted from zero). So set it to max objects per array amount value.
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=166f43d44e57b62da034bd9530713beb
Extract value from mysql json string stored as varchar
None of the answers helped resolve. So what I did was exported the table to csv, loaded to google sheet ( after splitting it into manageable size) and than used 'text to column' to extract the value from the json-like string.
After that, imported the csv into a temp table, tested and brought back live.
MySQL LIKE with json_extract
Okay, I was able to solve the case insensitivity by adding COLLATE utf8mb4_general_ci
after the LIKE
clause.
So the point here is to find a working collation, which in its turn can be found by researching the db you work with.
Related Topics
Retrieve Varbinary Value as Base64 in Mssql
Disable Secure Priv for Data Loading on MySQL
Sqlcmd Not Able to Find a Library (Libmsodbcsql-17.0.So.1.1) That Is There
Presto SQL - Converting a Date String to Date Format
Inserting Date Value into Date Field Using Laravel
Select to Get Rows Based on Minimum Value of a Column
How to Check If Value Is Inserted Successfully or Not
How to Get the Last 12 Months from the Current Date
How to Limit Results of a Left Join
Select Only Rows That Contain Only Alphanumeric Characters in MySQL
How to Select All Records from One Table That Do Not Exist in Another Table
How to Select the Last Record of a Table in SQL
Select All Dates Between First Day of Month and Current Date
How to Find Specific Values in a Table in Oracle
How to Add a Space Between Two Text in SQL Code
Storing Image in Database Directly or as Base64 Data