Junior — Middle
Is it possible to process and store data in JSON format within PostgreSQL?
sobes.tech AI
Answer from AI
Yes, PostgreSQL supports storing and processing data in JSON format. There are two main types for this in the database: json and jsonb.
jsonstores data in text format, validating JSON correctness upon insertion.jsonbstores data in binary format, allowing for faster queries and indexing.
Example of creating a table with a JSONB field:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
data JSONB
);
INSERT INTO users (data) VALUES ('{"name": "Ivan", "age": 30}');
SELECT data->>'name' AS name FROM users WHERE data->>'age' = '30';
In Java, to work with JSON in PostgreSQL, you can use JDBC and libraries like Jackson for serialization/deserialization, and pass JSON as a string in queries.