← Back to MySQL Course | Chapter 3: Data Types | Lesson 8 of 8

JSON Data Type

एक JSON column एक single cell में tucked एक mini folder जैसा है, labeled information का एक छोटा bundle रखते हुए, और MySQL ensure करता है कि bundle सही से लिखा गया है।
Syntax
sql
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

sql
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

sql
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

sql
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

sql
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

sql
SELECT JSON_ARRAY('red', 'green', 'blue') AS colors;
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. अंदर single quotes के साथ invalid JSON जैसे {theme: dark} insert करना, जिसे JSON column reject कर देता है।
  2. config->'$.theme' इस्तेमाल करना और plain text की उम्मीद करना, जबकि यह एक quoted JSON value return करता है (unquote करने के लिए ->> इस्तेमाल करें)।
  3. एक JSON path की शुरुआत में $ भूल जाना, जैसे '$.theme' के बजाय theme।
चैप्टर सारांश
  • MySQL में अलग-अलग तरह की values store करने के लिए numeric, string, और date व time data types हैं।
  • ENUM, SET, और BOOLEAN fixed choices और true या false values handle करते हैं।
  • BLOB और binary types raw data store करते हैं, और JSON type structured JSON documents store करता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

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