← Back to PostgreSQL Course | Chapter 10: Advanced Features | Lesson 6 of 7

JSONB की Basics

jsonb JSON documents को एक binary form में store करता है जिसे PostgreSQL index और query कर सकता है।

In this page:

  1. JSONB Basics
Syntax
sql
column_name jsonb
SELECT column -> 'key' FROM table_name;
SELECT column ->> 'key' FROM table_name;
WHERE column @> '{"key": "value"}'

JSONB Basics

jsonb type JSON को एक binary, indexable structure की तरह रखता है।

-> operator JSON return करता है, ->> text return करता है, @> containment test करता है, और ? एक key test करता है। jsonb_build_object, jsonb_set, और jsonb_agg documents बनाते और बदलते हैं, और GIN indexes containment queries fast बनाते हैं। json सिर्फ तभी इस्तेमाल करें जब आपको exact text formatting preserve करनी ज़रूरी हो।

Note: comparisons और display के लिए एक value को text की तरह पाने के लिए ->> इस्तेमाल करें।

उदाहरण: JSONB basics

bash
shop=# CREATE TABLE events (id serial PRIMARY KEY, data jsonb);
shop=# INSERT INTO events (data) VALUES ('{"type": "click", "user": {"name": "Ada", "age": 36}, "tags": ["a", "b"]}');
shop=# SELECT data->'user'->>'name' AS name, (data->'user'->>'age')::int AS age FROM events;
 name | age
------+-----
 Ada  |  36
shop=# SELECT id FROM events WHERE data @> '{"type": "click"}';
 id
----
  1
shop=# CREATE INDEX idx_events_data ON events USING GIN (data);

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. jsonb की बजाय json इस्तेमाल करना
  2. यह भूल जाना कि ->> text return करता है
  3. relational data को JSON की तरह store करना
चैप्टर सारांश
  • jsonb एक binary JSON type है
  • -> JSON return करता है, ->> text return करता है
  • @> containment test करता है
  • GIN indexes queries तेज़ करते हैं
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.