JSON data search in MySQL query - mysql

I have a MySQL column with below data:
{
"ids": [
"c9cfdecc-dfce-11eb-8c94-0ad691f61ea0",
"c9cfdecc-dfce-11eb-8c94-0ad691f61ea1",
"c9cfdecc-dfce-11eb-8c94-0ad691f61ea2"
]
}
How can I get records that matched with any requested string?
I tried below queries but no use:
SELECT * from test WHERE JSON_CONTAINS(fragments, '"c9cfdecc-dfce-11eb-8c94-0ad691f61ea1"', '$')
SELECT * from test WHERE JSON_CONTAINS(fragments, '"c9cfdecc-dfce-11eb-8c94-0ad691f61ea1"')
SELECT * from test WHERE JSON_SEARCH(fragments, 'one', "c9cfdecc-dfce-11eb-8c94-0ad691f61ea1")

Related

Convert select result that has JSON field to JSON and use that data with JSON_VALUE()

I have a table that has some columns.
One of these columns stores data in JSON format.
I select a row from this table with FOR JSON AUTO.
My problem is that SQL Server puts quotations around the value of JSON properties but I don't want this; because I want to use the values of inner JSON with JSON_VALUE(). What can I do?
Code:
SELECT TOP 1 *
FROM users;
Result:
name lastName age favorites
John Duo 20 {"city": "paris", "color": "blue", "sport": "football"}
Code:
SELECT TOP 1 *
FROM users
FOR JSON AUTO, WITHOUT_ARRAY_WRAPPER;
Result:
{"name":"John","lastName":"Duo","age":20,"favorites":"{\"city\": \"paris\", \"color\": \"blue\", \"sport\": \"football\"}"}
Code:
SELECT JSON_VALUE(#user_json,'$.favorites.color')
Result:
NULL
I can use this trick to get values from inner JSON, but it's not clean.
Code:
SELECT JSON_VALUE(JSON_VALUE(#user_json,'$.favorites'), '$.color')
Result:
blue
How can I do this in a clean way?
Some code for testing in SQL Server:
DROP TABLE IF EXISTS #test_tbl;
DECLARE #user_json AS NVARCHAR(MAX);
SELECT 'John' AS Name, 'Duo' AS LastName, 20 AS Age, '{"city": "paris", "color": "blue", "sport": "football"}' AS favorites
INTO #test_tbl;
SET #user_json =
(
SELECT *
FROM #test_tbl
FOR JSON AUTO, WITHOUT_ARRAY_WRAPPER
)
SELECT JSON_VALUE(#user_json,'$.favorites.color');
SELECT JSON_VALUE(JSON_VALUE(#user_json,'$.favorites'),'$.color');
You need to nest the favorites JSON using json_query(), e.g.:
SET #user_json =
(
SELECT Name, LastName, Age, json_query(favorites) as favorites
FROM #test_tbl
FOR JSON AUTO, WITHOUT_ARRAY_WRAPPER
)
SELECT JSON_VALUE(#user_json,'$.favorites.color');
# (No column name)
# ----------------
# blue

Select keys with boolean value true in PostgreSQL json query

In my PostgreSQL 11.6 table I have a json field app_settings with data on the following format:
{
"my_app": {
"features": {
"very_good_feature": true,
"awesome_feature": false,
"even_better_feature": true
}
}
}
I want to create a query that selects a list of feature names where the feature has the value true. So in the example above, I would like the result of the query simply to be 2 rows like this:
very_good_feature
even_better_feature
I can successfully select ALL the keys with the following query:
select
json_object_keys(app_settings ->'my_app' -> 'features') as k
from
my_table;
How can I write the where clause to only list the ones with true as value?
try with json_each_text:
select j.* from my_table
join lateral json_each_text(app_settings->'my_app'->'features') j(k, v) on true
where
j.v = 'true'

Find match item in JSON array

I have a SQL table called 'Interactions' and a column called Events that holds an array of JSON data. I am looking for any row where the Events json has any array with #odata.type = #Sitecore.XConnect.Goal
[
{
"#odata.type":"#Sitecore.XConnect.Collection.Model.PageViewEvent",
"CustomValues":[
],
"DefinitionId":"9326cb1e-cec8-48f2-9a3e-91c7dbb2166c",
"ItemId":"5ae02a76-ac59-4dbb-913f-3b2e51d92bae",
"Id":"b067f72f-1d60-4773-a2e8-9d23770fea54",
"Timestamp":"2019-11-11T17:18:31.2206225Z",
"ItemLanguage":"en",
"ItemVersion":1,
"Url":"/renewal-confirmation",
"SitecoreRenderingDevice":{
"Id":"fe5d7fdf-89c0-4d99-9aa3-b5fbd009c9f3",
"Name":"Default"
}
},
{
"#odata.type":"#Sitecore.XConnect.Goal",
"CustomValues":[
],
"DataKey":"/sitecore/content/Client/home/renewal-confirmation",
"DefinitionId":"c0bc91b9-b5fc-4faa-bc12-3dba8bbae44f",
"ItemId":"5ae02a76-ac59-4dbb-913f-3b2e51d92bae",
"EngagementValue":100,
"Id":"e7031fb4-ce57-459f-a9f1-2682748d3431",
"ParentEventId":"b067f72f-1d60-4773-a2e8-9d23770fea54",
"Timestamp":"2019-11-11T17:18:31.2518732Z"
}
]
What I am currently trying is this, but no results
select *
from [Interactions] I
where exists
(
select *
from openjson(I.Events,'$."#odata.type"')
where value = '#Sitecore.XConnect.Goal'
)
If I use this, I can get any row where the 1st item in the array #odata.type = #Sitecore.XConnect.Goal. This works. But I want it from any item in the array.
SELECT *
FROM [Interactions]
WHERE JSON_VALUE([Events], '$[0]."#odata.type"') = '#Sitecore.XConnect.Goal'
One possible approach is to parse the JSON column using OPENJSON() with explicit schema (WITH clause with columns definitions). With this approach you can filter the Interactions table and get information from the JSON data.
Table:
CREATE TABLE Interactions (
Events nvarchar(max)
)
INSERT INTO Interactions
(Events)
VALUES
(N'[
{
"#odata.type":"#Sitecore.XConnect.Collection.Model.PageViewEvent",
"CustomValues":[
],
"DefinitionId":"9326cb1e-cec8-48f2-9a3e-91c7dbb2166c",
"ItemId":"5ae02a76-ac59-4dbb-913f-3b2e51d92bae",
"Id":"b067f72f-1d60-4773-a2e8-9d23770fea54",
"Timestamp":"2019-11-11T17:18:31.2206225Z",
"ItemLanguage":"en",
"ItemVersion":1,
"Url":"/renewal-confirmation",
"SitecoreRenderingDevice":{
"Id":"fe5d7fdf-89c0-4d99-9aa3-b5fbd009c9f3",
"Name":"Default"
}
},
{
"#odata.type":"#Sitecore.XConnect.Goal",
"CustomValues":[
],
"DataKey":"/sitecore/content/Client/home/renewal-confirmation",
"DefinitionId":"c0bc91b9-b5fc-4faa-bc12-3dba8bbae44f",
"ItemId":"5ae02a76-ac59-4dbb-913f-3b2e51d92bae",
"EngagementValue":100,
"Id":"e7031fb4-ce57-459f-a9f1-2682748d3431",
"ParentEventId":"b067f72f-1d60-4773-a2e8-9d23770fea54",
"Timestamp":"2019-11-11T17:18:31.2518732Z"
}
]')
Statement:
SELECT *
FROM Interactions i
CROSS APPLY OPENJSON(i.Events) WITH (
ODataType nvarchar(100) '$."#odata.type"'
-- and additional columns definitions
) j
WHERE j.ODataType = '#Sitecore.XConnect.Goal'
Your original query is close but doesn't work because you can't directly pick out scalars with OPENJSON. You want something like this:
SELECT *
FROM [interactions]
WHERE EXISTS (
SELECT 1
FROM OPENJSON([events]) WITH (
[#odata.type] NVARCHAR(MAX)
)
WHERE [#odata.type] = '#Sitecore.XConnect.Goal'
)

Selecting all objects in a json array that meet criteria in mysql

I need to capture rows when data inside a json field in mysql meets certain criteria. What select would I run to get the rows returned where the json column had a Status value of 1000 in any object in the array?
I tried using the something like this:
SELECT * FROM table WHERE Task_JSON->"$[0]" = 1000;
I'm using Server version: 5.7.11 of mysql
[
{
"Sequence":"1" ,
"Status":"1000"
},
{
"Sequence":"2" ,
"Status":"1000"
},
{
"Sequence":"3" ,
"Status":"3000"
}
]
You can use the following solution using JSON_CONTAINS:
SELECT *
FROM table_name
WHERE JSON_CONTAINS(Task_JSON, '{"Status":"1000"}') > 0;
Another solution using JSON_SEARCH:
SELECT *
FROM table_name
WHERE JSON_SEARCH(Task_JSON, 'one', '1000', NULL, '$[*]."Status"') IS NOT NULL;
demo: https://www.db-fiddle.com/f/3JTQreVi63FS7HbmWQ9TEM/4
SELECT * FROM table WHERE Task_JSON ->>'Status' = 1000;
JSON_EXTRACT can help you.
https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html
select * from table where JSON_EXTRACT(Task_JSON, '$.Status') = 1000 ;

MySQL JSON - SELECT WHERE

I'm trying to SELECT objects base on the roles property values.
Example: Select all names where role is 1 //response would return danny
Query Statement:
SELECT JSON_EXTRACT(username,'$[*].name') FROM objects WHERE JSON_CONTAINS(username,'1','$[*].roles')
COLUMN: username (JSON)
[
{
"name":"jordan",
"roles":[1,2,5]
},
{
"name":"danny",
"roles":[1,4]
}
]
Question: Why isn't my statement returning just the first object containing the name danny?
Try the following:
SELECT *
FROM objects
WHERE JSON_CONTAINS(components, '1', '$.roles');