I have a column data consisting of {"name":["John","Peter"],id:["20","30"]}
If I do
SELECT JSON_VALUE(data,'$.name[0]') from table
it returns John but doing
SELECT JSON_VALUE(data,'$') from db
SELECT JSON_VALUE(data,'$.name') from table
returns NULL in both.
How come it does not return:
{"name":["John","Peter"],id:["20","30"]}
["John","Peter"]
As mentioned in the remarks section of the JSON_VALUE documentation there is a table that says for tags array in the json says: Use JSON_QUERY instead.
SELECT json_query(j,'$.name') from a;
Fiddle
Related
I have a JSON column and the data stored looks like:
{"results":{"made":true,"cooked":true,"eaten":true}}
{"results":{"made":true,"cooked":true,"eaten":false}}
{"results":{"made":true,"eaten":true,"a":false,"b":true,"c":false}, "more": {"ignore":true}}
I need to find all rows where 1+ values in $.results is false.
I tried using JSON_CONTAINS() but didn't find a way to get it to compare to a boolean JSON value, or to look at all values in $.results.
This needs to work with MySQL 5.7 but if it's not possible I will accept a MySQL 8+ answer.
I don't know the way for to search for a JSON true/false/null value using JSON functions - in practice these values are treated as string-type values during the search with JSON_CONTAINS, JSON_SEARCH, etc.
Use regular expression for the checking. Something like
SELECT id,
JSON_PRETTY(jsondata)
FROM test
WHERE jsondata REGEXP '"results": {[^}]+: false.*}';
DEMO
You could simply search the JSON_EXTRACT using the LIKE condition this way.
SELECT * FROM table1 WHERE JSON_EXTRACT(json_dict, '$.results') LIKE '%: false%';
Check this DB FIDDLE
An alternative to the pattern matching in other answers, is to extract all values from $.results and check each entry with a helper table with running numbers
SELECT DISTINCT v.id, v.json_value
FROM (
SELECT id, json_value, JSON_EXTRACT(json_value, '$.results.*') value_array
FROM json_table
) v
JOIN seq ON seq.n < JSON_LENGTH(v.value_array)
WHERE JSON_EXTRACT(v.value_array, CONCAT('$[', seq.n, ']')) = false
Here is the demo
I have a column data consisting of {"name":["John","Peter"],id:["20","30"]}
If I do
SELECT JSON_VALUE(data,'$.name[0]') from table
it returns John but doing
SELECT JSON_VALUE(data,'$') from db
SELECT JSON_VALUE(data,'$.name') from table
returns NULL in both.
How come it does not return:
{"name":["John","Peter"],id:["20","30"]}
["John","Peter"]
As mentioned in the remarks section of the JSON_VALUE documentation there is a table that says for tags array in the json says: Use JSON_QUERY instead.
SELECT json_query(j,'$.name') from a;
Fiddle
Hello i have a question about mysql json_extract function.
DROP TABLE IF EXISTS `db`.`PacJSON`;
CREATE TABLE PacJSON SELECT * FROM PAC;
ALTER TABLE PacJSON ADD COLUMN hc JSON DEFAULT NULL;
UPDATE `db`.`PacJSON`
SET `hc`='[{"estado":"1"},{"descripcion":"name1"},{"estudio": "name2"}, {"url":"name3"},{"idpaciente":"11"},{"idmedico":"6"},{"fecha_hc":"2019-05-2"}]'
WHERE idUsuario=11;
The query below is returning a null value when it should return 6.
Can somebody tell me what am i doing wrong?
SELECT idUsuario,PacJSON.hc ,JSON_UNQUOTE(JSON_EXTRACT(hc ,'$."idmedico"')) AS "idmedico"
FROM PacJson;
Thanks!
Your hc value contains an array of objects, so you need to use a path which reflects that i.e. $[*].idmedico. Note that double quotes should not be used in the path.
SELECT idUsuario,
hc,
JSON_UNQUOTE(JSON_EXTRACT(hc ,'$[*].idmedico')) AS idmedico
FROM PacJSON
Output:
["6"]
Note that because hc is an array, JSON_EXTRACT returns one too. If you know there's only one idmedico value in the JSON, you can use JSON_EXTRACT again on that result to get the actual numeric value:
SELECT idUsuario,
hc,
JSON_UNQUOTE(JSON_EXTRACT(JSON_EXTRACT(hc ,'$[*].idmedico'), '$[0]')) AS idmedico
FROM PacJSON
Output:
6
Demo on dbfiddle
I’m trying to make the following sql statement in a json file:
SELECT user_id
FROM users_to_users
WHERE (user_id, contact_user_id, contact_blocked) IN ?
I tried providing an array of arrays for the ? Values but that returned an error. Is there a way with json to pass an array of tuples for the inserts in the above sql statement?
Simple Example:-
Use the FIND_IN_SET function:
SELECT t.*
FROM YOUR_TABLE t
WHERE FIND_IN_SET(3, t.ids) > 0
-- all books with tags starting 'Java':
SELECT * FROM `book`
WHERE JSON_SEARCH(tags, 'one', 'Java%') IS NOT NULL;
Check This Reference which uses JSON To see the whole possible Solutions
Reference Of FIND_IN_SET Answer
I have the follwing simple MySQL query that returns 0 results:
SELECT d.name FROM document d WHERE 10 IN (d.categories)
"categories" is of type varchar and contains for example "10,20,30".
When I type the IN values directly it works and returns a result:
SELECT d.name FROM document d WHERE 10 IN (10,20,30)
I suspect MySQL substitutes d.documents with something like this which of course is not what I want:
SELECT d.name FROM document d WHERE 10 IN ("10,20,30")
What is a proper workaround for this?
When you are providing the value as "10,20,30" then it is treated as a single value as against your expected three distinct values. To make the value as three distinct values you need to use the find_in_set function in MySQL.
Also I would suggest you to go through this thread: Is storing a delimited list in a database column really that bad?
Yes with find_in_set :
SELECT d.name FROM document d WHERE find_in_set(10, d.categories) > 0