PostgreSQL JSONB Query Builder
Querying a jsonb column means remembering which arrow returns json and which returns text, how to reach into nested objects and arrays, and — the part that usually bites — which index actually helps. Enter your table, jsonb column and a dotted path like user.address.city, choose an operator, and this builder generates the SELECT with correctly quoted keys, the matching GIN or B-tree expression index for that specific operator, and a cookbook of the JSONB operations you reach for next: expanding arrays with jsonb_array_elements, updating single keys with jsonb_set, and SQL/JSON path queries. All generated locally in your browser.
Do more than postgresql jsonb query builder — meet Chat2DB
Chat2DB is an AI-powered SQL client for Windows, macOS and Linux. Write SQL in natural language, format and optimize queries automatically, and manage MySQL, PostgreSQL, Oracle and 20+ other databases in one workspace.
How to use
- Enter the table and jsonb column, then a path such as user.address.city or items[0].sku — array indexes and nested keys are both supported.
- Pick the operator you need (text equality, numeric comparison, JSON containment, key existence) and type the value to match.
- Copy the generated SELECT, then create the index shown below it — the tool picks GIN or a B-tree expression index based on the operator you chose.
Frequently asked questions
What is the difference between -> and ->> in PostgreSQL?
-> returns a jsonb value, so you can keep chaining into it: payload->'user'->'address' is still jsonb. ->> returns text, which ends the chain but gives you a value you can compare with =, pass to ILIKE, or cast with ::numeric. The rule of thumb is to use -> for every level except the last, and ->> on the final key. If you compare with ->, remember the right side has to be jsonb too — payload->'age' = '30'::jsonb, not = 30.
How do I index a JSONB column in PostgreSQL?
It depends on the operator. For containment and key-existence queries (@>, ?, ?|, ?&) create a GIN index: CREATE INDEX ... USING gin (payload), or USING gin (payload jsonb_path_ops) for a smaller, faster index that supports @> only. A GIN index does not help a query like payload->>'city' = 'Berlin' — for that you need a B-tree expression index: CREATE INDEX ... ON events ((payload->>'city')). The expression in the index must match the one in your WHERE clause exactly, or the planner will ignore it.
Should I use json or jsonb in PostgreSQL?
Use jsonb in almost every case. json stores an exact copy of the input text and reparses it on every single access, preserves key order and duplicate keys, and cannot be indexed with GIN. jsonb stores a decomposed binary form: slightly slower to insert, much faster to query, deduplicates keys, and supports the full operator and indexing surface. Only pick json when you must round-trip the original document byte for byte. To explore jsonb documents and test these queries against a live database with AI-assisted SQL, try Chat2DB: download at https://chat2db.ai/download or use https://app.chat2db.ai.
