I have a table which contains a column "owners", which has json data in it like this:
[
{
"first":"bob",
"last":"boblast"
},
{
"first":"mary",
"last": "marylast"
}
]
I would like to write a query that would return for each row that contains data like this, a column that has all of the first names concatenated with a comma.
i.e.
id owners
----------------------------
1 bob,mary
2 frank,tom
Not on mysql 8.0 yet.
You can get the values as a JSON array:
SELECT JSON_EXTRACT(owners, '$[*].first') AS owners ...
But that returns in JSON array format:
+-----------------+
| owners |
+-----------------+
| ["bob", "mary"] |
+-----------------+
JSON_UNQUOTE() won't take the brackets and double-quotes out of that. You'd have to use REPLACE() as I show in a recent answer here:
MYSQL JSON search returns results in square brackets
You should think about not storing data in JSON format if it doesn't support the way you need to query them.
Here is another option, get a helper table with running numbers up to the max json array length, and extract values by individual index, after that group_concat the values, something like this:
SELECT g.id, GROUP_CONCAT(g.name)
FROM (
SELECT a.id, JSON_UNQUOTE(JSON_EXTRACT(a.owners, CONCAT('$[', n.idx, '].first'))) name
FROM running_numbers n
JOIN mytable a
) g
GROUP BY g.id
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=d7453c9edf89f79ca4ab2f63578b320c
Related
I want to create two columns from a column of values containing JSON in Snowflake using SQL.
Say this table is called keywords_bids
then there is a column called keywords that has JSON in it
example json in a cell in the keywords column:
row1: {"apple":0.1, "peach":0.2, "banana":0.1} row2: similar JSON, etc....
input image
I want to create a columns called keyword and it is bid price from the JSON
output would be:
keyword | Bid
'apple' | 0.1
'peach' | 0.2
'banana'| 0.3
First for JSON you'll need to change the single quotes to double quotes.
Then you just need to flatten the json to get keys and values:
with data as (
select parse_json('{"apple":0.1, "peach":0.2, "banana":0.1}') j
)
select k.key, k.value
from data, table(flatten(j)) k
;
https://community.snowflake.com/s/article/Dynamically-extracting-JSON-using-LATERAL-FLATTEN
This article is to demonstrate various examples of using LATERAL FLATTEN to extract information from a JSON Document. Examples are provided for its utilization together with GET_PATH, UNPIVOT, and SEQ functions.
I have some db that stores json array of objects in a column. I need to make columns out of these objects keys if object's id has certain value
My documents table is simple:
id | name | percent_split
1 | some name | [ {id: 59, share:22, value: 55},{id:58, share:40, value:33}]
I've tried some variants of the following to get the data, not mentioning making columns out of object keys:
SELECT
id, name,
(SELECT FROM documents
WHERE (
CASE WHEN JSON_VALID(percent_split) THEN
json_search(percent_split, 'one', '{"id":"59"}')
ELSE null
END)
) AS result_for_59
FROM documents
I would like to get additional columns in the result of my query
id, name, 59_share, 59_value, NN_share, NN_value
Please help :)
I could go with any language easily, but in this case I'm limited to use only MySql technology.
We made a function in mysql which has two arguments: the column_value and the value we search for:
RETURN JSON_EXTRACT(value, JSON_UNQUOTE(
REPLACE(
JSON_SEARCH(
,'one',column_value
),'.id','.share'
))
)
Then we call it inside select part of query, for every value we need:
if(json_valid(percent_split),get_share(39, percent_split),NULL) AS share39
I have a table that is a single column made up of a JSON string. The JSON has multiple key pairs, and is a string because it is the raw table.
One of the keys is "Ticket" and has dollar amount values. I am not certain if prices are in __.__ format, or just ____. I want to query the column to return me the entire string if this "Ticket" ends in a 6, as in 96 cents, or 66 cents, etc.
This is my query:
SELECT json FROM tablename
WHERE json RLIKE '%"TICKET": "___6",%'
OR json RLIKE '%"TICKET": "__._6",%'
This currently returns as blank.
How can I get the entire string if the dollar amount ends in a 6 (as in 6 cents)?
The search strings you are using are what you would use for LIKE
So you could use LIKE :
select * from tablename
where (json LIKE '%"TICKET": "___6"%' or json LIKE '%"TICKET": "__._6"%')
Or a RLIKE with a regex:
select * from tablename
where json RLIKE '"TICKET":[ ]*"[0-9.]+6"'
I have table with many columns that contains string array json of objects.
I need to remove several elements from these arrays.
I find how can I remove elements from one row, but how can I do it on multiple rows?
For one row I used json_search to find element that must be removed, but I have many rows and many elements to remove. Is there any way to do it without stored procedure (while loop)?
This is sample of data:
-------------------------------------------------------------------
id | DATA |
-------------------------------------------------------------
1 | {"array":[{"a":"a","b":"b","c":"c"},{"b":"b","c":"c"}]}|
------------------------------------------------------------
2 | {"array":[{"b":"b","c":"c","f":"f"},{"b":"b","c":"c","d":"d"}]}|
-------------------------------------------------------------------
3 | {"array":[{"a":"a","b":"b","c":"c"},{"g":"g","ff":"ff"}]}|
4 | {"array":[{"q":"q"},{"g":"f","e":"e"}]}|
I need to remove only elements from each array that contans a and/or g
My query is:
UPDATE MY_TABLE
SET DATA = JSON_REMOVE(
DATA,
REPLACE(JSON_SEARCH(
(SELECT DATA WHERE DATA LIKE "%a%"),
'all',
"%a%"
),
'"',
'')
) WHERE DATA LIKE "%a%";
I found the way how to update all columns, but this query removes only json field, not whole object. How can I remove whole object?
Found the way.
The data was in "$.array[5].a" format after calling JSON_SEARCH. I just needed to wrap replace into another replace that will replace '.a' to just ''.
I have a json object in my database table field like following :
[{"user_id":"xyz","viewed":"false","answered":"false","denied":"false"}].
the other fields are id, q_id and stuff.
I want to search for the user xyz in the table!how can i do that using mysql?
JSON Parsing in MySQL Using Common_schema
Try following query using common schema
select * from tablename where common_schema.extract_json_value(tablename.columnName,'/user_id') = 'xyz';
Reference: http://mechanics.flite.com/blog/2013/04/08/json-parsing-in-mysql-using-common-schema/
If the object pattern is same across then you can use the substring_index function to parse the data, below is the example of finding the user_id from this pattern
mysql> select replace(substring_index(substring_index('[{"user_id":"xyz","viewed":"false","answered":"false","denied":"false"}]','"user_id":',-1),',',1),'"','') as user_id;
+---------+
| user_id |
+---------+
| xyz |
+---------+
Now if you want to select all the rows having user_id = xyz you can use the above as
select * from table_name
where replace(substring_index(substring_index('[{"user_id":"xyz","viewed":"false","answered":"false","denied":"false"}]','"user_id":',-1),',',1),'"','') = 'xyz';
thank you for your answers. But i just used a like query and it worked
SELECT viewers from tablename where viewers like '%\"user_id\":\"xyz\"%' && qid= 1;
In mysql there is a functionality for dba called common schema. It has json functionality.
Or
use find_in_set() function to find coma seperated values in json.