← Back to MySQL Course | Chapter 19: Advanced & Reference | Lesson 2 of 5

MySQL JSON Functions

JSON functions एक cell में एक छोटे labeled bundle के अंदर पहुँचकर सिर्फ एक item पढ़ने या बदलने के tools जैसे हैं बिना पूरी चीज़ खोले।
Syntax
sql
JSON_EXTRACT(json_column, '$.key')
json_column->'$.key'
JSON_OBJECT('key1', value1, 'key2', value2)

JSON Columns के साथ काम करना

MySQL एक single column के अंदर पूरा JSON document store कर सकता है, जो optional या loosely structured data — जैसे किसी product के variable attributes — के लिए एक अच्छा fit है जो fixed relational columns पर cleanly map नहीं होता।

उदाहरण: Working with JSON Columns

sql
CREATE TABLE products (id INT, attributes JSON);
INSERT INTO products VALUES (1, '{"color": "red", "size": "M"}');

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

JSON Values Extract करना

JSON_EXTRACT, या ज़्यादा concise inline arrow operator, आपको सीधे एक query के अंदर एक JSON column से एक specific value निकालने देता है, इसलिए आप पूरा document application में load किए बिना nested JSON data पर filter या select कर सकते हैं।

उदाहरण: Extracting JSON Values

sql
CREATE TABLE products (id INT, attributes JSON);
INSERT INTO products VALUES (1, '{"color": "red", "size": "M"}');
SELECT id, JSON_EXTRACT(attributes, '$.color') AS color FROM products;
SELECT id, attributes->'$.color' AS color FROM products;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

JSON Data बनाना

raw JSON text हाथ से लिखने के बजाय, MySQL functions देता है जो relational values — numbers, strings, दूसरे columns — को आपके लिए directly सही से formatted JSON output में assemble करते हैं।

उदाहरण: Creating JSON Data

sql
SELECT JSON_OBJECT('color', 'red', 'size', 'M') AS built_json;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

JSON Data Modify करना

JSON_SET एक मौजूदा JSON document के अंदर एक value in place update करता है, या तो एक मौजूदा key overwrite करके या एक नई add करके, बिना आपको पूरा document replace करने की ज़रूरत के।

उदाहरण: Modifying JSON Data

sql
CREATE TABLE products (id INT, attributes JSON);
INSERT INTO products VALUES (1, '{"color": "red"}');
UPDATE products SET attributes = JSON_SET(attributes, '$.size', 'M') WHERE id = 1;
SELECT * FROM products;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

JSON Key Values से Query करना

एक JSON column के अंदर दबी एक value के आधार पर rows filter करना लगभग उतना ही efficiently काम करता है जितना एक normal column पर filter करना, खासकर एक बार जब आपके सबसे ज़्यादा query किए जाने वाले JSON path पर एक generated column या index set up हो जाए।

उदाहरण: Querying by JSON Key Values

sql
CREATE TABLE products (id INT, attributes JSON);
INSERT INTO products VALUES (1, '{"color": "red"}'), (2, '{"color": "blue"}');
SELECT * FROM products WHERE JSON_EXTRACT(attributes, '$.color') = 'red';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

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. invalid JSON text insert करना, जिसे JSON column reject कर देता है।
  2. JSON_EXTRACT इस्तेमाल करना और unquoted text की उम्मीद करना, जबकि यह quotes वाली एक JSON value return करता है (->> इस्तेमाल करें)।
  3. $ के बिना $.color लिखना, या गलत case के साथ, क्योंकि JSON keys case-sensitive हैं।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 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.