0

I have column in postgreSQL with json data type. Until today there were not row which contained {} or [].

However, I start to see {} and [] due to new implementation. I want to remove it.

Example: Following is my table looks like. json is json data type

 id |       json       
----+------------------
  a | {"st":[{"State": "TX", "Value":"0.02"}, {"State": "CA", "Value":"0.2" ...
----+------------------
 b  | {"st":[{"State": "TX", "Value":"0.32"}, {"State": "CA", "Value":"0.47" ...
----+------------------
 d  | {}
----+------------------
 e  | []

Where I want as following:

 id |       json       
----+------------------
  a | {"st":[{"State": "TX", "Value":"0.02"}, {"State": "CA", "Value":"0.2" ...
----+------------------
 b  | {"st":[{"State": "TX", "Value":"0.32"}, {"State": "CA", "Value":"0.47" ...

How I should able to do it ?

I have writen following query:

SELECT *
FROM tableA
WHERE json::text <> '[]'::text 

Where I am able to filter empty elements which starts with {}. but still seeing [].

3
  • 1
    In other words: you want to remove empty elements? Commented Jul 25, 2016 at 14:55
  • Yes, I want to remove empty elements. I don't know why it is coming but when I want to query it, I don't want empty elements. Commented Jul 25, 2016 at 14:56
  • 3
    The real question is: why are you sometimes inserting an empty JSON object and sometimes and empty JSON array. Commented Jul 25, 2016 at 15:04

1 Answer 1

2

Very easy, just select all rows that don't contain those values:

SELECT *
FROM tableA
WHERE json :: text NOT IN ('{}', '[]')
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.