0

I have a jsonb value

{"mts": "375", "tele2": "", "beeline": "56", "megafon": "377"}

how a can check for example that mts and megafon is not empty

my query is

select * 
from test 
where settings->>'megafon' <> '' 
   or settings->>'mts' <> '' 
   or settings->>'beeline' <> ''

But it is so longer query, how to write short condition?

1
  • Do you really store keys with "empty" values, or would the keys simply not be part of the JSON? e.g. would your JSON look like this {"mts": "", "tele2": "", "beeline": "56", "megafon": "377"} or like this: {"tele2": "", "beeline": "56", "megafon": "377"} Commented Oct 10, 2018 at 8:08

1 Answer 1

2
SELECT * FROM
    jsonb_each_text('{"mts": "", "tele2": "", "beeline": "56", "megafon": "377"}'::jsonb)
WHERE value != ''

json_each_text() expands every json element as text element into one row each. The result is a table with columns key and value.

Now you are able to filter the value column for every content.

https://www.postgresql.org/docs/current/static/functions-json.html

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.