MySQL Wildcard on JSON array - mysql

I'm trying to extract an ID (5384) that is inside an array contained in JSON using wildcards. The problem I'm having is that the position of the ID doesn't have a fixed position for each element in that array. An example of my JSON in that array is like this (where "id":5384 could occupy different indexed positions):
{
"id":7465115,
"name":"BCA_WS_FBX_Nielsen PRIZM_Test_Unlock_1x1",
"advertiser_id":155085,
"pixels":[
{
"id":416491,
"pixel_template_id":null,
},
{
"id":5384,
"pixel_template_id":null,
}
]
}
My query is as follows:
SELECT id, json FROM PROD_APPNEXUS.dimension_json_creatives
WHERE JSON LIKE ('%pixels%_%"id":5384,%') AND MEMBER_ID = 364
I'm trying to extract only items that are in the pixels array and have an ID of 5384.
Any comments as to how to achieve this would be highly valued, thanks!
UPDATE: MySQL version 5.6.17
Sam

Try:
12.6 The JSON Data Type. MySQL 5.7.8+
SELECT
`id`,
`json` -- Data Type JSON
FROM
`PROD_APPNEXUS`.`dimension_json_creatives`
WHERE
JSON_CONTAINS(`json`, '{"id": 5384}', '$.pixels') AND
`MEMBER_ID` = 364;
UPDATE
Using 5.6.17, one option is:
SELECT
`id`,
`json`
FROM
`PROD_APPNEXUS`.`dimension_json_creatives`
WHERE
`json` REGEXP '"pixels":\\[.*"id":5384.*]' AND
MEMBER_ID = 364;
Performance may be affected depending on the number of tuples in the table.

The only way I would think of is to use the REGEXP syntax of MySQL:
SELECT id, json FROM PROD_APPNEXUS.dimension_json_creatives
WHERE (JSON REGEXP '("pixels":\[)?.*"id":5384') AND MEMBER_ID = 364

Try with:
SELECT
`id`,
JSON_EXTRACT(json, '$.pixels') as pixels
FROM
`PROD_APPNEXUS`.`dimension_json_creatives`
WHERE
JSON_CONTAINS(`json`, '{"id": 5384}', '$.pixels') AND
`MEMBER_ID` = 364;
Please note that you need a version >= 5.7.8 for MySQL for this to work

Related

Extract all values from a PostgreSQL JSON array given a key

How exctract from json in postgres
[{"val":"2","dsk:"one"},{"val":"2","dsk":"two"},{"val":"3","dsk":"three"}]
where dsk values
It return null values
SELECT '[{"val":"2","dsk:"one"},{"val":"2","dsk":"two"},{"val":"3","dsk":"three"}]'::json->'dsk'
You can use the jsonb_path_query_array function and extract the entire value from the array
select jsonb_path_query_array('[{"val":"2","dsk":"one"},{"val":"2","dsk":"two"},{"val":"3","dsk":"three"}]','$[*].dsk')
Demo in DBfiddle
As mentioned you cannot use your approach because it is an array, but you can try a different one with a json function:
WITH data
AS (
SELECT *
FROM json_array_elements('[{"val":"2","dsk":"one"},{"val":"2","dsk":"two"},{"val":"3","dsk":"three"}]'::json)
)
SELECT value->'dsk'
FROM data

Check if boolean value present in nested object

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

how to ORDER BY json object in MySQL

i'm trying to execute a query to give me 10 rows with the most biggest score ,column score in my table is a json object like :
{fa="7",en="7"}
how can i set my query to order by this json object ( it doesn't matter which of them (en or fa) used because they are always same )
Assuming your json is {"fa"="7","en"="7"} and assuming your json are in my_json_col column you could access using a -> operator and order by
SELECT *
from my_table
order by my_json_col->"fa"

How can I pass array of tuples to sql statement using json

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

Query json column based on the integer value in Postgres

I am using postgres v9.3
I have a table called temp which have a column all_data. The value looks something like below :-
{"Accountid" : "1364", "Personalid" : "4629-87c3-04e6a7a60208", "quote_number" : "QWQA62364384"}
Now, I want to query the all_data column by accountid=1364.
Could you please tell what would be the query?
Use the ->> operator
select *
from temp
where all_data ->> 'Accountid' = '1364';
Online example for 9.3: http://sqlfiddle.com/#!15/55981/1
However the above will not work if the JSON contains an array instead of a JSON object. E.g. a value like '[1,2]'::json will cause that query to fail.
With 9.4 and above you could check that using json_typeof() but with your soon to be unsupported version the only workaround I can think of is to convert the column to text and exclude those that start with a [
with valid_rows as (
select *
from temp
where all_data::text not like '[%'
)
select *
from valid_rows
where all_data ->> 'Accountid' = '1364';
Online example for 9.3: http://sqlfiddle.com/#!15/d01f43/3