MySQL JSON Functions
In this page:
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
CREATE TABLE products (id INT, attributes JSON);
INSERT INTO products VALUES (1, '{"color": "red", "size": "M"}');
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
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;
JSON Data बनाना
raw JSON text हाथ से लिखने के बजाय, MySQL functions देता है जो relational values — numbers, strings, दूसरे columns — को आपके लिए directly सही से formatted JSON output में assemble करते हैं।
उदाहरण: Creating JSON Data
SELECT JSON_OBJECT('color', 'red', 'size', 'M') AS built_json;
JSON Data Modify करना
JSON_SET एक मौजूदा JSON document के अंदर एक value in place update करता है, या तो एक मौजूदा key overwrite करके या एक नई add करके, बिना आपको पूरा document replace करने की ज़रूरत के।
उदाहरण: Modifying JSON Data
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;
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
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';
- invalid JSON text insert करना, जिसे
JSONcolumn reject कर देता है। JSON_EXTRACTइस्तेमाल करना और unquoted text की उम्मीद करना, जबकि यह quotes वाली एक JSON value return करता है (->>इस्तेमाल करें)।$के बिना$.colorलिखना, या गलत case के साथ, क्योंकि JSON keys case-sensitive हैं।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: