B
B
Bogatyr2019-10-09 10:49:49
MySQL
Bogatyr, 2019-10-09 10:49:49

How to get data from JSON by string length?

Hello to all!
Please tell me how to make a selection from JSON? JSON is the following:

{"@class": "Name.Of.Class, "member": "666", "Members": [{"values": ["111", "222", "333"], "Type": "BIG", "Number": "111111", "Count": 50, "BIGUnit": 50}, {"values": ["111", "222", "333"],"BIGType":"BIG","Number": "111", "Count": 10, "BIGUnit": 10}, {"values": ["111", "222", "333"], "BIGType": "BIG", "Number": "222", "Count": 10, "BIGUnit": 10}, {"values": ["111", "222", "333"], "BIGType": "BIG","Number": "333", "Count": 10, "BIGUnit": 10}, {"values": ["111", "222", "333"], "BIGType": "BIG", "Number": "333333", "Count": 10, "BIGUnit": 10}], "ID": "07"}

It is necessary to return all values ​​of Number > 3.
I tried this script:
SELECT
c_date AS cd,
JSON_UNQUOTE(JSON_EXTRACT(jsonc,"$.Members[*].Number")) AS rep,
id AS did
FROM reports
WHERE length(JSON_UNQUOTE(JSON_EXTRACT(jsonc,"$.Members[*].Number"))) > 3;

c_date - Date, jsonc - column with JSON, id is id.
Moreover, if the condition is removed, then the script returns ALL Number values, and only those that > ​​3 are needed. And if with a condition, then it returns NULL.
Help me please!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Bogatyr, 2019-10-10
@Bogatyr

SELECT cte1.cdate AS cd, 
       JSON_UNQUOTE(JSON_EXTRACT(cte1.rep, CONCAT('$[', cte2.num, ']'))) AS rep, 
       cte1.id AS did
FROM ( SELECT cdate, 
              id, 
              JSON_EXTRACT(jsonc,"$.Members[*].Number") AS rep
       FROM reports ) cte1, 
     ( SELECT help_keyword_id num
       FROM mysql.help_keyword ) cte2
HAVING LENGTH(rep) > 3

Z
zhaar, 2019-10-09
@zhaar

And you need to consider the number condition as a string or a number? Now, according to the condition, it takes away from you everything that contains at least 4 characters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question