JSON Data Type
In this page:
column_name JSON
SELECT column_name->'$.key' FROM table_name;
JSON Columns
Native JSON column type एक structured document को सीधे एक single column में store करता है और automatically validate करता है कि आप जो भी insert करें वह syntactically valid JSON हो, malformed input को पूरी तरह reject करते हुए।
उदाहरण: JSON Columns
CREATE TABLE settings (config JSON);
INSERT INTO settings VALUES ('{"theme": "dark"}');
JSON Data Extract करना
-> operator (या ज़्यादा verbose JSON_EXTRACT function) आपको एक query में सीधे एक stored JSON document से एक specific field निकालने देता है, application code में पूरे document को पढ़े और parse किए बिना।
उदाहरण: Extracting JSON Data
CREATE TABLE settings (config JSON);
INSERT INTO settings VALUES ('{"theme": "dark"}');
SELECT config->'$.theme' FROM settings;
JSON Documents Modify करना
JSON_SET और JSON_INSERT आपको अपने application से पूरे JSON blob को fetch, modify, और rewrite किए बिना एक stored document के अंदर एक single key in place update या add करने देते हैं।
उदाहरण: Modifying JSON Documents
CREATE TABLE settings (config JSON);
INSERT INTO settings VALUES ('{"theme": "dark"}');
UPDATE settings SET config = JSON_SET(config, '$.theme', 'light');
JSON Validation
JSON_VALID() check करता है कि कोई arbitrary string well-formed JSON है या नहीं, और JSON_CONTAINS_PATH() check करता है कि किसी document में एक specific path exist करता है या नहीं — दोनों external input पर भरोसा करने से पहले उपयोगी guards हैं।
उदाहरण: JSON Validation
SELECT JSON_VALID('{"a": 1}') AS is_valid, JSON_CONTAINS_PATH('{"a": 1}', 'one', '$.a') AS has_path;
JSON Arrays
JSON_ARRAY() और related functions आपको एक SELECT statement के अंदर सीधे एक JSON array बनाने देते हैं, जो query results को एक ऐसे format में shape करने के लिए handy है जिसे एक API endpoint जैसा है वैसा return कर सके।
उदाहरण: JSON Arrays
SELECT JSON_ARRAY('red', 'green', 'blue') AS colors;
- अंदर single quotes के साथ invalid JSON जैसे
{theme: dark}insert करना, जिसेJSONcolumn reject कर देता है। config->'$.theme'इस्तेमाल करना और plain text की उम्मीद करना, जबकि यह एक quoted JSON value return करता है (unquote करने के लिए->>इस्तेमाल करें)।- एक JSON path की शुरुआत में
$भूल जाना, जैसे'$.theme'के बजायtheme।
- MySQL में अलग-अलग तरह की values store करने के लिए numeric, string, और date व time data types हैं।
ENUM,SET, औरBOOLEANfixed choices और true या false values handle करते हैं।BLOBऔर binary types raw data store करते हैं, औरJSONtype structured JSON documents store करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: