0

The database version mariadb 10.3.16 I have a JSON of objects labels: an array of strings

So when I am querying just the labels value, I have the result

["label1", "label7"]
["label2", "labeltest"]
["label1", "labeltest"]

How can I extract only distinct values from here?

1 Answer 1

3

You can use recursive CTEs and JSON functions to extract the label values.

Here's an example query that extracts the unique values from JSON arrays in a table:

WITH RECURSIVE labels AS (
  SELECT 0 AS depth, JSON_VALUE(data, CONCAT('$[', 0, ']')) AS data
    FROM t1
    WHERE JSON_LENGTH(t1.data) > 0
  UNION
  SELECT l.depth + 1, JSON_VALUE(t1.data, CONCAT('$[', l.depth + 1, ']')) AS data
    FROM t1, labels as l
    WHERE JSON_LENGTH(t1.data) - 1 > l.depth
)
SELECT data FROM labels;
Sign up to request clarification or add additional context in comments.

Comments

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.