Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query for null values in json field type postgresql?

I have a json type field in postgresql. However I can't select rows where specific field is null:

Code:

SELECT * FROM   json_array_elements(   '[{"name": "Toby", "occupation": "Software Engineer"},     {"name": "Zaphod", "occupation": "Galactic President"} , {"name2": "Zaphod", "occupation2": null} ]'  ) AS elem where elem#>'{occupation2}' is null 

This should work but I am getting this error:

ERROR:  operator does not exist: json #> boolean LINE 6: where elem#>'{occupation2}' is null 
like image 438
Alexandru R Avatar asked Oct 17 '13 09:10

Alexandru R


People also ask

How do I query JSON data type in PostgreSQL?

Querying JSON dataPostgreSQL provides two native operators -> and ->> to help you query JSON data. The operator -> returns JSON object field by key. The operator ->> returns JSON object field by text.

How check value is NULL in JSON?

To find the difference between null and undefined, use the triple equality operator or Object is() method. To loosely check if the variable is null, use a double equality operator(==). The double equality operator can not tell the difference between null and undefined, so it counts as same.

How do I SELECT NULL values in PostgreSQL?

Example - With INSERT Statement INSERT INTO contacts (first_name, last_name) SELECT first_name, last_name FROM employees WHERE employee_number IS NULL; This PostgreSQL IS NULL example will insert records into the contacts table where the employee_number contains a NULL value.

Can JSON be NULL Postgres?

Expands a JSON array to a set of text values. Returns the type of the outermost JSON value as a text string. Possible types are object, array, string, number, boolean, and null.


2 Answers

you can use the fact that elem->'occupation2' returns string null of type json, so your query will be:

select     * from  json_array_elements(   '[{"name": "Toby", "occupation": "Software Engineer"},     {"name": "Zaphod", "occupation": "Galactic President"} ,     {"name2": "Zaphod", "occupation2": null} ]' ) as elem where (elem->'occupation2')::text = 'null'  {"name2": "Zaphod", "occupation2": null} 

If you want to get all elements where value is null in JSON or key doesn't exists, you can just do:

select     * from  json_array_elements(   '[{"name": "Toby", "occupation": "Software Engineer"},     {"name": "Zaphod", "occupation": "Galactic President"} ,     {"name2": "Zaphod", "occupation2": null} ]' ) as elem where (elem->>'occupation2') is null  {"name": "Toby", "occupation": "Software Engineer"} {"name": "Zaphod", "occupation": "Galactic President"} {"name2": "Zaphod", "occupation2": null} 
like image 191
Roman Pekar Avatar answered Sep 19 '22 05:09

Roman Pekar


If you are searching for a null value within a json-blob you might want to consider using the function json_typeof(json) that was introduced in Postgres 9.4:

INSERT INTO table   VALUES ('{ "value": "some", "object": {"int": 1, "nullValue": null}}');  SELECT * FROM table   WHERE json_typeof(json->'object'->'nullValue') = 'null'; 

This will result in you finding your entry for the null value.

Hope this helps!

Reference: http://www.postgresql.org/docs/9.4/static/functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE

like image 22
mraxus Avatar answered Sep 19 '22 05:09

mraxus