0

I am using MariaDB 10.5, I have a JSON field in a table which typical contains something like:

[{"Filename":"C:/Users/simon/Documents/file.ext"},{"Soil":"Sand"},{"Present":"None"},{"Value":"5000"}]

I want to construct queries that can look for matches in the database, so I might have a QStringList containing:

Soil:Sand
Value:5000

Where the value left of the colon is the JSON member name and the value on the right is the value to compare.

How can I use these to construct a query where the table name is for example: datasets and the field is jsonParams ?

nbk
8,6996 gold badges15 silver badges27 bronze badges
asked Jun 8, 2021 at 9:24

2 Answers 2

1

Use JSON_SEARCH().

...
WHERE JSON_SEARCH(value, 'one', 'Sand', NULL, '$[*].Soil') IS NOT NULL

Your method with JSON_VALUE() is applicable if there is only one "Soil" attribute per the whole value.

See DEMO fiddle

answered Jun 8, 2021 at 11:25
1
  • There is only one of each in each object. Commented Jun 9, 2021 at 5:41
0

Done...I used the function JSON_VALUE like so:

SELECT
 biPK
FROM
 datasets
WHERE
 JSON_VALUE(jsonParams, '$[*].Soil')='Sand'

The [*] above tells it to look for any index in an array for the term 'Soil' and compare with 'Sand'.

answered Jun 8, 2021 at 10:48
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.