MAnI
MAnI

Reputation: 47

MySql JSONObject Ifnull

I am working on Mysql query to retrieve the JSON Object

Select JSON_OBJECT('id', students.id, 'name', students.name) as test from students;

output


         "test": {
            "id": null,
            "name": null
          }

expected output

test: null

How can i check if test object has a null values then return null

Upvotes: 0

Views: 474

Answers (1)

Akina
Akina

Reputation: 42844

How can i check if test object has a null values then return null

Formally (without understanding the logic):

SELECT CASE WHEN students.id IS NULL AND students.name IS NULL
            THEN CAST( 'null' AS JSON )
            ELSE JSON_OBJECT('id', students.id, 'name', students.name) 
            END AS test 
FROM students;

Upvotes: 1

Related Questions