Understanding JSON Syntax: The 7 Core Rules
When writing JSON (JavaScript Object Notation), following the correct syntax is crucial to ensuring your data is well-formatted and valid. Regardless of the data types you are working with, building a JSON file relies on seven fundamental rules.
File Extension Basics
Before diving into the syntax itself, always remember to save your JSON files using the .json extension (similar to how XML files use .xml). This tells the system and other developers that the file contains JSON data.
The 7 Rules of JSON Syntax
- Rule 1: Data is written inside curly braces
{}Everything you write must be enclosed within a parent set of curly braces. Data written outside, above, or alongside these parent braces will result in an invalid JSON file. Stray punctuation marks like loose semicolons or colons outside the braces are also strictly prohibited. - Rule 2: Data is represented as Key-Value pairsJSON structures data similarly to a hash map. You must always define a “key” paired with a specific “value.” Writing lone strings or values without a corresponding key is invalid formatting.
- Rule 3: Keys must be enclosed in double quotes
""Every key you define must be wrapped in double quotes (e.g.,"product_id"). While values may not always need them (such as when defining an integer), keys strictly require double quotes to avoid errors. - Rule 4: Keys and Values are separated by a colon
:You cannot use an equals sign (=) or leave a blank space to assign a value. The standard and only acceptable format is"key": value. - Rule 5: Data pairs are separated by a comma
,When adding multiple key-value pairs to your JSON file, separate each pair with a comma.
Crucial Formatting Note: Do not place a comma after the last key-value pair in an object. Commas are strictly separators, not line terminators. - Rule 6: Square brackets
[]hold arraysIf you need to define a list of items (for example, a list of available"colors"for a phone), enclose those values within square brackets. - Rule 7: Curly braces
{}hold nested objectsTo define a new object inside your JSON data (like listing a subset of countries where a phone was released), you use curly braces. Just like the parent JSON, this nested object must follow the key-value pair rule, requiring a key to define the object itself.
Here is a sample product.json file that demonstrates all seven rules of JSON syntax discussed in the class notes:
JSON
{
"product_id": 101,
"name": "SmartPhone Pro",
"is_available": true,
"price": 999.99,
"colors": [
"Midnight Black",
"Glacier White",
"Ocean Blue"
],
"release_details": {
"year": 2026,
"global_launch": true,
"regions": ["North America", "Asia", "Europe"]
}
}
How this code applies the 7 rules:
- Rule 1 (Curly braces): The entire document is enclosed in the outermost
{ }. - Rule 2 (Key-Value pairs): Data is structured in pairs, such as
"name"(key) and"SmartPhone Pro"(value). - Rule 3 (Double quotes for keys): Every key, like
"product_id","price", and"colors", is wrapped in double quotes. - Rule 4 (Colon separator): A colon
:separates every key from its value (e.g.,"price": 999.99). - Rule 5 (Comma separator): Commas
,separate the different data pairs. Notice there is no comma after the last item ("Ocean Blue"in the array, or"regions"in the object). - Rule 6 (Square brackets for arrays): The
"colors"and"regions"keys hold multiple values enclosed in[ ]. - Rule 7 (Curly braces for objects): The
"release_details"key holds a nested object, enclosed in its own set of{ }.
JSON సింటాక్స్ను అర్థం చేసుకోవడం: 7 ముఖ్యమైన నియమాలు
JSON (జావాస్క్రిప్ట్ ఆబ్జెక్ట్ నొటేషన్) రాసేటప్పుడు, మీ డేటా సరైన ఫార్మాట్లో మరియు చెల్లుబాటు అయ్యేదిగా (valid) ఉందని నిర్ధారించుకోవడానికి సరైన సింటాక్స్ను పాటించడం చాలా ముఖ్యం. మీరు ఏ డేటా రకాలతో (data types) పనిచేస్తున్నప్పటికీ, JSON ఫైల్ను రూపొందించడం ఏడు ప్రాథమిక నియమాలపై ఆధారపడి ఉంటుంది.
ఫైల్ ఎక్స్టెన్షన్ ప్రాథమికాంశాలు
సింటాక్స్లోకి వెళ్లే ముందు, మీ JSON ఫైల్లను ఎల్లప్పుడూ .json ఎక్స్టెన్షన్ ఉపయోగించి సేవ్ చేయాలని గుర్తుంచుకోండి (XML ఫైల్లు .xml ఎలా ఉపయోగిస్తాయో అదే విధంగా). ఫైల్లో JSON డేటా ఉందని ఇది సిస్టమ్కు మరియు ఇతర డెవలపర్లకు తెలియజేస్తుంది.
JSON సింటాక్స్ యొక్క 7 నియమాలు
- నియమం 1: డేటా కర్లీ బ్రేసెస్
{}లోపల రాయబడుతుందిమీరు రాసే ప్రతిదీ ఒక జత కర్లీ బ్రేసెస్ (parent set of curly braces) లోపల ఉంచాలి. ఈ బ్రేసెస్కు బయట, పైన లేదా పక్కన రాసిన డేటా వల్ల అది ఇన్వాలిడ్ (invalid) JSON ఫైల్ అవుతుంది. బ్రేసెస్కు వెలుపల సెమికోలన్లు లేదా కోలన్లు వంటి అదనపు గుర్తులు ఉండటం కూడా ఖచ్చితంగా నిషిద్ధం. - నియమం 2: డేటా కీ-వాల్యూ పెయిర్స్ (Key-Value pairs) గా సూచించబడుతుందిJSON డేటాను హ్యాష్ మ్యాప్ (hash map) లాగా స్ట్రక్చర్ చేస్తుంది. మీరు ఎల్లప్పుడూ ఒక నిర్దిష్ట “వాల్యూ (value)” తో జత చేయబడిన “కీ (key)” ని డిఫైన్ చేయాలి. దానికి సంబంధించిన కీ లేకుండా ఒంటరి స్ట్రింగ్లు లేదా వాల్యూలను రాయడం ఇన్వాలిడ్ ఫార్మాటింగ్ అవుతుంది.
- నియమం 3: కీలు తప్పనిసరిగా డబుల్ కోట్స్
""లో ఉంచాలిమీరు డిఫైన్ చేసే ప్రతి కీ తప్పనిసరిగా డబుల్ కోట్స్లో ఉండాలి (ఉదాహరణకు,"product_id"). వాల్యూలకు అవి ఎల్లప్పుడూ అవసరం లేనప్పటికీ (ఉదాహరణకు ఇంటిజర్ను డిఫైన్ చేసేటప్పుడు), ఎర్రర్లను నివారించడానికి కీలకు మాత్రం డబుల్ కోట్స్ ఖచ్చితంగా అవసరం. - నియమం 4: కీ మరియు వాల్యూలు కోలన్
:తో వేరు చేయబడతాయివాల్యూను కేటాయించడానికి మీరు ఈక్వల్స్ గుర్తు (=) వాడకూడదు లేదా ఖాళీ స్థలాన్ని వదలకూడదు."key": valueఅనేది ప్రామాణికమైనది మరియు ఆమోదయోగ్యమైన ఏకైక ఫార్మాట్. - నియమం 5: డేటా పెయిర్స్ కామా
,తో వేరు చేయబడతాయిమీ JSON ఫైల్కు బహుళ కీ-వాల్యూ పెయిర్స్ను జోడిస్తున్నప్పుడు, ప్రతి పెయిర్ను కామాతో వేరు చేయండి.
ముఖ్యమైన ఫార్మాటింగ్ గమనిక: ఆబ్జెక్ట్లో చివరి కీ-వాల్యూ పెయిర్ తర్వాత కామాను ఉంచవద్దు. కామాలు కేవలం వేరు చేయడానికి మాత్రమే (separators), లైన్ ముగింపుకి (terminators) కాదు.
- నియమం 6: అరే (arrays) లను ఉంచడానికి స్క్వేర్ బ్రాకెట్లు
[]వాడతారుమీరు ఐటెమ్ల జాబితాను డిఫైన్ చేయవలసి వస్తే (ఉదాహరణకు, ఫోన్ కోసం అందుబాటులో ఉన్న"colors"జాబితా), ఆ వాల్యూలను స్క్వేర్ బ్రాకెట్ల లోపల ఉంచండి. - నియమం 7: నెస్టెడ్ ఆబ్జెక్ట్లను (nested objects) ఉంచడానికి కర్లీ బ్రేసెస్
{}వాడతారుమీ JSON డేటా లోపల కొత్త ఆబ్జెక్ట్ను డిఫైన్ చేయడానికి (ఫోన్ విడుదలైన దేశాల సబ్సెట్ను జాబితా చేయడం వంటివి), మీరు కర్లీ బ్రేసెస్ను ఉపయోగిస్తారు. పేరెంట్ JSON లాగానే, ఈ నెస్టెడ్ ఆబ్జెక్ట్ కూడా కీ-వాల్యూ పెయిర్ నియమాన్ని పాటించాలి, అంటే ఆబ్జెక్ట్ను డిఫైన్ చేయడానికి తప్పనిసరిగా ఒక కీ అవసరం.