5

I have the following row on select

jsonData
[]
[{"descricao":"falha na porta"}, {"descricao":"falha no ip"}]
[]

I have to Identify empty jsons, then manually add a value to it (eg row 1 and 3 ), I tried the following :

case when jsonData is null then cast('[{"descricao":"no error"}]' AS json) else jsonData   end as opts

But the "is null" verification fails for this type of data (array of json), how to identify '[]' values in this case?

Note: I only have select permission on this db

3 Answers 3

5

Try this condition:

jsondata = JSON '[]'
Sign up to request clarification or add additional context in comments.

3 Comments

ERROR: operator does not exist: json = jsonb Hint: No operator matches the given name and argument types, also the same occurs when using these: jsondata = JSON '[]' and jsondata = '[]' :: json
However worked using jsondata::text = '[]' , Your suggestion helped me reach this answer thanks.
@RenanFernandes: the other option is to cast your json column to jsonb (which it should be anyways): jsondata::jsonb = jsonb '[]'
4

You can use json_array_length()

when json_array_length(jsondata) = 0 then ...

Comments

1

Casting the json to text before comparison worked for this case : " case when jsondata::text = '[]' "

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.