1

I have this JSON structure in a field in a table in a MariaDB database:

table column name: BcDJSON

{"placards":
     [
     {"barcode":"???","destination":"???","weight":"???"}, 
     {"barcode":"???","destination":"???","weight":"???"},
     {etc..}
    ]
}


my sql query current looks like:

"SELECT * from table WHERE BcDJSON LIKE '%.$value.%'";

but that does not result in stable code. I have tried using JSON_EXTRACT, but going through an object into an array of objects in MariaDB: I have tried a few variations - and reading through the docs has not answered my question. is JSON_EXTRACT even what I really want to use here? I need to return the whole row - where a barcode exists in that JSON structure.

syntaxs things I have tried: (??? = valid values)

SELECT * from datawork WHERE JSON_EXTRACT('BcDJSON', '$.barcode') = '???'
SELECT * from datawork WHERE JSON_EXTRACT('BcDJSON', '$.placards, $.barcode') = '???'
SELECT * from datawork WHERE JSON_EXTRACT('BcDJSON', '$.placards.$.barcode') = '???'
SELECT * from datawork WHERE JSON_EXTRACT('BcDJSON', '$.placards->$.barcode') = '???'
SELECT * from datawork WHERE JSON_EXTRACT('BcDJSON', '$.placards$.barcode') = '???'
SELECT * from datawork WHERE JSON_EXTRACT('BcDJSON', '$.placards') = '???'
SELECT * from datawork WHERE JSON_EXTRACT('BcDJSON', '$.placards', '$.barcode') = '???'
SELECT * from datawork WHERE JSON_EXTRACT('BcDJSON', '$.placards.barcode') = '???'


3
  • Which version of MySQL? Commented Jan 20, 2020 at 23:48
  • Server version: 10.3.16-MariaDB PHP: 7.3.6 Commented Jan 20, 2020 at 23:55
  • 1
    I changed the mysql tag to mariadb, and edited your title and question. MariaDB support for JSON is quite different from MySQL. Commented Jan 21, 2020 at 1:47

1 Answer 1

1

You should be using JSON_SEARCH to see if the value can be found in one of the barcode values:

SELECT * 
FROM datawork
WHERE JSON_SEARCH(BcDJSON, 'one', '???', NULL, '$.placards[*].barcode') IS NOT NULL

For example:

CREATE TABLE datawork (id int auto_increment primary key,
                       BcDJSON TEXT);
INSERT INTO datawork (BcDJSON) values 
('{"placards":
     [
     {"barcode":"123","destination":"a","weight":"1"}, 
     {"barcode":"456","destination":"b","weight":"2"}
    ]
}'),
('{"placards":
     [
     {"barcode":"789","destination":"a","weight":"1"}, 
     {"barcode":"123","destination":"b","weight":"2"}
    ]
}');
SELECT * 
FROM datawork 
WHERE JSON_SEARCH(BcDJSON, 'one', '123',  NULL,'$.placards[*].barcode') IS NOT NULL

Output:

id  BcDJSON
1   {"placards": [ {"barcode":"123","destination":"a","weight":"1"}, {"barcode":"456","destination":"b","weight":"2"} ] }
2   {"placards": [ {"barcode":"789","destination":"a","weight":"1"}, {"barcode":"123","destination":"b","weight":"2"} ] }

Demo on dbfiddle

Sign up to request clarification or add additional context in comments.

6 Comments

#1210 - Incorrect arguments to ESCAPE not yet getting that to work: keep getting this error in the first section of code. the 'one' parameter is? i understand the name[*].prop notation there.
@altruios I've updated my demo to be more similar to your situation
@altruios the 'one' parameter simply tells JSON_SEARCH to stop looking as soon as it finds a single match. That parameter can also be 'all', which will return paths of all matching barcodes. Since you only want an existence check, 'one' is sufficient.
no more errors on the sql side, but returning empty results when I copy and paste a barcode it has no results. tried multiple values.
after looking more into the problem - found the answer - turns out it wasn't returning anything because of a white space that was not being cleared on insertion. fixed and working now! well, now onto figuring out how to trim all the white spaces from all the barcode entries in that json structure.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.