Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
SQL

SQL: Use JSON Functions to Query JSON Columns

- 05.06.26 - ErcanOPAK

📊 NoSQL in SQL, Best of Both Worlds

Store flexible data as JSON. Query inside JSON. Index specific fields. PostgreSQL JSON functions give you relational + document database.

📝 Store and Query JSON

CREATE TABLE products (
    id SERIAL PRIMARY KEY,
    name TEXT,
    metadata JSONB
);

INSERT INTO products (name, metadata) VALUES 
('Laptop', '{"brand": "Apple", "ram": 16, "colors": ["space-gray", "silver"]}'),
('Phone', '{"brand": "Samsung", "ram": 8, "colors": ["black", "white"]}');

-- Query JSON fields
SELECT name, metadata->>'brand' as brand
FROM products
WHERE metadata->>'brand' = 'Apple';

-- Nested access
SELECT name, metadata->'colors'->>0 as first_color
FROM products;

-- Search in JSON array
SELECT name
FROM products
WHERE metadata->'colors' ? 'space-gray';

🎯 JSON Operators
-- -> : Get as JSON (with quotes)
-- ->> : Get as text (without quotes)
-- #> : Get nested by path
-- #>> : Get nested as text

-- Check if key exists
SELECT * FROM products WHERE metadata ? 'brand';

-- Check if any in array
SELECT * FROM products WHERE metadata->'colors' ? 'space-gray';

-- Update JSON (atomic)
UPDATE products
SET metadata = metadata || '{"warranty": "2 years"}'
WHERE id = 1;

-- Remove key
UPDATE products
SET metadata = metadata - 'warranty'
WHERE id = 1;

💡 Indexing JSON

  • GIN index on entire JSON: CREATE INDEX idx_gin ON products USING GIN (metadata);
  • Index specific key: CREATE INDEX idx_brand ON products ((metadata->>'brand'));
  • B-tree on extracted path for equality searches

“Products have 50 optional attributes. 50 columns would be sparse. JSONB column: clean, queryable, indexable. PostgreSQL’s JSON support is better than MongoDB.”

— Database Architect

Related posts:

List all Foreign Keys referencing a given table in SQL Server

SQL Tables Age Poorly

Return number of rows affected by UPDATE statements in SQL

Post Views: 4

Post navigation

.NET Core: Use Source Generators for Compile-Time Code Generation
C#: Use Generic Math for Mathematical Algorithms on Any Numeric Type

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

June 2026
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  
« May    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (953)
  • How to add default value for Entity Framework migrations for DateTime and Bool (882)
  • Get the First and Last Word from a String or Sentence in SQL (838)
  • How to select distinct rows in a datatable in C# (808)
  • How to make theater mode the default for Youtube (805)
  • Add Constraint to SQL Table to ensure email contains @ (580)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (579)
  • Average of all values in a column that are not zero in SQL (538)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (505)
  • Find numbers with more than two decimal places in SQL (454)

Recent Posts

  • C#: Use String Interpolation Instead of Concatenation
  • C#: Use Tuples to Return Multiple Values from Methods
  • SQL: Use ISNULL and NULLIF for Smart NULL Handling
  • .NET Core: Use Data Annotations for Model Validation
  • Git: Use Git Clean to Remove Untracked Files
  • Ajax: Add Custom Headers to Fetch Requests
  • JavaScript: Use console.table to Display Arrays as Tables
  • HTML: Use Spellcheck Attribute to Enable Browser Spell Check
  • CSS: Use user-select to Prevent Text Selection
  • Windows 11: Use Snipping Tool for Instant Screenshots

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (953)
  • How to add default value for Entity Framework migrations for DateTime and Bool (882)
  • Get the First and Last Word from a String or Sentence in SQL (838)
  • How to select distinct rows in a datatable in C# (808)
  • How to make theater mode the default for Youtube (805)

Recent Posts

  • C#: Use String Interpolation Instead of Concatenation
  • C#: Use Tuples to Return Multiple Values from Methods
  • SQL: Use ISNULL and NULLIF for Smart NULL Handling
  • .NET Core: Use Data Annotations for Model Validation
  • Git: Use Git Clean to Remove Untracked Files

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com