27

Imagine a table which tracks baseball pitchers like so...

     +------------+--------------------+-------+
     | id | name               | secondary_pitch |
     +------------+--------------------+-------+
     | 13 | Chris Sale         | ['Curveball','Slider'] |
     | 14 | Justin Verlander   | ['Fastball','Changeup'] |
     | 15 | CC Sabathia        | ['Fastball','Curveball'] |
     | 16 | Sonny Grey         |    ['Slider'] |
     | 17 | Aldoris Chapman    |    [] |
     +------------+--------------------+-------+

Notice the secondary_pitch column has a JSON value. So if a pitcher, like Chapman, has no secondary pitch, it will not return null, instead it returns an empty JSON string ('[]').

How then can I get a count of the number of pitchers who have no secondary pitch?

I can't do...

  select count(*) from pitchers where secondary_pitch is null
2
  • What's the benefit of putting a Json file in your column? (I would redesign the table) Commented Jun 30, 2018 at 12:10
  • 2
    All the "array types" like ['Curveball','Slider'] are invalid JSON so NONE of MySQL's native JSON (like JSON_LENGTH, JSON_SEARCH or JSON_EXTRACT saw a couple of answers with it) functions would work... ["Curveball","Slider"] is valid JSON then the MySQL native JSON functions should work just fine. Commented Jun 30, 2018 at 12:21

5 Answers 5

60

I think you can just use json_length():

where json_length(secondary_pitch) = 0
Sign up to request clarification or add additional context in comments.

4 Comments

Wont work the data (for example ['Curveball','Slider']) is invalid JSON..
@RaymondNijland Why? It works perfectly. List with 0 items have 0 length in json_length
"Why? It works perfectly" Why? @jsnceo because ["Curveball", "Slider"] is valid JSON check it on jsonlint.com ['Curveball','Slider'] does not valid here you should not risk using invalid JSON annywhere in anny programming language even if it "works" MySQL should have given a error on ['Curveball','Slider'].. In short the string values and key in a JSON array or Object needs to be double quoted thats the JSON standard
What you're looking at is how mySQL is representing the result of a select. Everything here is valid.
13

You could use JSON_EXTRACT to get first value from your column and check for not null

where JSON_EXTRACT(`secondary_pitch`, '$[0]') is not null

Demo

2 Comments

@RaymondNijland check the demo link, it will work with double quotes not with single quote
The data with double qoutes is indeed valid JSON then the JSON function works. You should mention in the answer that the topicstarters current JSON data is invalid JSON
6

I see this is not answering original question of matching against empty array ([]) but this has worked for me, matching against empty dictionary ({}), at mysql 5.7.20-0ubuntu0.16.04.1 - (Ubuntu).

I used JSON_OBJECT function but it is very likely the JSON_ARRAY will also work in similar way, creating the 'empty' object when called without arguments.

If I wanted to match against the json column vmkeys value of {} (empty dictionary), I used the following query:

SELECT vmkeys FROM `labinstances` WHERE vmkeys=JSON_OBJECT() 

To match against the vmkeys value of NULL, I used this:

SELECT vmkeys FROM `labinstances` WHERE vmkeys is NULL 

Hope this helps...

1 Comment

Thank you! Updating to proper nulls: update items set tags = null where tags = JSON_OBJECT();
1

This will check where secondary_pitch is (null) or '' (empty string)

SELECT count(*) from pitchers WHERE secondary_pitch IS NULL OR secondary_pitch = '';

also you can use like this.

SELECT count(*) from pitchers WHERE secondary_pitch LIKE '%[]%'

4 Comments

a JSON ([]) empty json array element is not equal to NULL or ' ' so this won't work..
So you can use it like this "LIKE '%[]%' "
I chose this answer because all the other answers required a JSON function which I don't have access to since I have read only permissions on that DB[Code: 1370, SQL State: 42000] execute command denied to user 'jjreadonly'@'%' for routine 'turnto_summary.JSON_SEARCH'
Downvoted because this is not working (tested with MySQL 8.0.15).
0

You can use the below function

SELECT * FROM TABLE_NAME WHERE 
JSON_SEARCH(secondary_pitchers, 'all', '') 
    IS NOT NULL;

1 Comment

Wont work the data (for example ['Curveball','Slider']) is invalid JSON..

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.