0

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

1
  • Your "expected output" is not valid JSON. From the looking point of MySQL at least. Commented Apr 20, 2021 at 10:22

1 Answer 1

1

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;
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.