2

How do I search [] where in json like

{"buyer_uni_no":{"info":[],"Pos":[[0,0,0,0]]}}

And there are my sql query I try but didn't work.

select * from test where JSON_VALUE(myjson,'$.buyer_uni_no.info') = '[]'
select * from test where JSON_VALUE(myjson,'$.buyer_uni_no.info[0]') = ''
3
  • 2
    probably you need to use JSON_LENGTH, ie select * from test where JSON_LENGTH(myjson,'$.buyer_uni_no.info') = 0 Commented Apr 26, 2019 at 5:53
  • @IłyaBursov you should add that as an answer. Looks like that'll give OP the needed records. Commented Apr 26, 2019 at 5:58
  • @IłyaBursov It's work!Thank you~ Commented Apr 26, 2019 at 6:32

1 Answer 1

1

Try using the JSON_LENGTH function on the array:

SELECT *
FROM test
WHERE JSON_LENGTH(json->"$.buyer_uni_no.info") = 0;

Demo

Another way, checking for a literal empty array []:

SELECT *
FROM test
WHERE CAST(myjson->"$.buyer_uni_no.info" AS CHAR(50)) = '[]';
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer! JSON_LENGTH is work! But (json->"$.buyer_uni_no.info") need to change to (json,'$.buyer_uni_no.info').
What I gave above should at least run on MySQL 8+. Maybe you are using an earlier version of MySQL where the JSON API be slightly different.

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.