PostgreSQL's JSON data type allows the database to parse, validate, and index JSON data. This makes querying parts of the JSON efficient and reliable, unlike plain text which is just stored as a string.
CREATE TABLE users (id SERIAL PRIMARY KEY, profile JSON); INSERT INTO users (profile) VALUES ('{"age": 30, "city": "Boston"}'); INSERT INTO users (profile) VALUES ('{"age": 25, "city": "Seattle"}');
The query filters users where the 'age' key in the JSON equals '30' (as text). It then selects the 'city' value from the JSON, which is 'Boston' for that user.
The ->> operator extracts the JSON value as text, which is usually what you want when reading JSON fields. Option D returns JSON type, B is invalid syntax, and D misses quotes around the key.
GIN indexes are designed to index JSON data efficiently, allowing fast lookups on keys and values inside JSON columns. Other options are less efficient or add complexity.
The ->> operator returns text, so comparing it directly with a number using > causes an error. Casting the text to numeric fixes this.