The goal is to return all records where a certain key is missing from a JSON data type. Just to make it obvious, I'm trying to query the JSON structure for anything where faultyField returns null among the following table data:
id::SERIAL | data::JSON
------------+-------------------
1 | {"key" : "val"}
2 | {"key1" : "val2"}
Any of the following two statements:
SELECT * FROM test WHERE data->>'faultyField' = null;
SELECT * FROM test WHERE (data->>'faultyField')::text = 'null';
Should return all values (according to two SO posts I found at work)? But it doesn't for some reason. These two just return empty results.
I'm no wizard with PostgreSQL so I've tried everything in between these statements below just to see if they work at all. Sadly they doesn't.
SELECT * FROM test WHERE (SELECT json_object_keys(data) FROM test)='key';
SELECT * FROM test WHERE 'key' in json_object_keys(data);
SELECT * FROM test WHERE 'key' := json_object_keys(data);
SELECT * FROM test WHERE 'key' ANY (json_object_keys(data));
Is there a way to return records where a key is missing from the JSON string?