Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract value without quotation mark from MySQL JSON data type

Tags:

json

mysql

I have started using the JSON data type in mysql 5.7. Is there a way to extract a value without the quotation marks? For instance when setting up a virtual index.

Example:

mysql> INSERT INTO test (data) VALUES ('{"type": "user" , 
"content" : { "username": "jdoe", "firstname" : "John", "lastname" : "Doe" } }');

mysql> SELECT json_extract(data,'$.type') FROM test;
+-----------------------------+
| json_extract(data,'$.type') |
+-----------------------------+
| "user"                      |
+-----------------------------+

How to get

+-----------------------------+
| json_extract(data,'$.type') |
+-----------------------------+
| user                        |
+-----------------------------+

?

like image 998
Willem van Gerven Avatar asked Apr 29 '16 08:04

Willem van Gerven


People also ask

What is JSON extract () function in MySQL?

We can use the JSON_EXTRACT function to extract data from a JSON field. The basic syntax is: JSON_EXTRACT(json_doc, path) For a JSON array, the path is specified with $[index] , where the index starts from 0: mysql> SELECT JSON_EXTRACT('[10, 20, 30, 40]', '$[0]'); +------------------------------------------+

How do I unquote in MySQL?

In MySQL, the JSON_UNQUOTE() function “unquotes” a JSON document and returns the result as a utf8mb4 string. You provide the JSON document as an argument, and the function will do the rest.

What is Json_extract?

JSON_EXTRACT. Extracts a JSON value, such as an array or object, or a JSON scalar value, such as a string, number, or boolean. JSON-formatted STRING or JSON. JSON_EXTRACT_SCALAR. Extracts a scalar value.

Which of the following functions create JSON values?

Two aggregate functions generating JSON values are available. JSON_ARRAYAGG() returns a result set as a single JSON array, and JSON_OBJECTAGG() returns a result set as a single JSON object.


3 Answers

You can use ->> operator to extract unquoted data, simply!

SELECT JSONCOL->>'$.PATH' FROM tableName

Two other ways:

  • JSON_UNQUOTE(JSON_EXTRACT(column, path))
  • JSON_UNQUOTE(column->path)

Note: Three different ways yield to the same command, as EXPLAIN explains:

As with ->, the ->> operator is always expanded in the output of EXPLAIN, as the following example demonstrates:

EXPLAIN SELECT c->>'$.name' AS name FROM jemp WHERE g > 2 ;
SHOW WARNINGS ;
*************************** 1. row ***************************
Level: Note
Code: 1003
Message: /* select#1 */ select
json_unquote(json_extract(`jtest`.`jemp`.`c`,'$.name')) AS `name` from
`jtest`.`jemp` where (`jtest`.`jemp`.`g` > 2)
1 row in set (0.00 sec)

read more on MySQL Reference Manual https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html#operator_json-inline-path

Note: The ->> operator was added in MySQL 5.7.13

like image 105
MohaMad Avatar answered Oct 09 '22 13:10

MohaMad


You can use JSON_UNQUOTE() method:

SELECT JSON_UNQUOTE(json_extract(data,'$.type')) FROM test;

This method will deal with internal quotes, for instance:

SET @t1 := '{"a": "Hello \\\"Name\\\""}';
SET @j := CAST(@t1 AS JSON);
SET @tOut := JSON_EXTRACT(@j, '$.a');
SELECT @t1, @j, @tOut, JSON_UNQUOTE(@tOut), TRIM(BOTH '"' FROM @tOut);

will give:

@t1     : {"a": "Hello \"Name\""}
@j      : {"a": "Hello \"Name\""}
@tOut   : "Hello \"Name\""
unquote : Hello "Name"
trim    : Hello \"Name\

I believe that the unquote is better in almost all circumstances.

like image 63
ivan88 Avatar answered Oct 09 '22 14:10

ivan88


MySQL 8.0.21 supports JSON_VALUE function

Extracts a value from a JSON document at the path given in the specified document, and returns the extracted value, optionally converting it to a desired type. The complete syntax is shown here:

JSON_VALUE(json_doc, path [RETURNING type] [on_empty] [on_error])
on_empty:
       {NULL | ERROR | DEFAULT value} ON EMPTY
on_error:
       {NULL | ERROR | DEFAULT value} ON ERROR

If not specified by a RETURNING clause, the JSON_VALUE() function's return type is VARCHAR(512)

db<>fiddle demo

SELECT json_value(data,'$.type')
FROM test;
-- user
like image 8
Lukasz Szozda Avatar answered Oct 09 '22 15:10

Lukasz Szozda