What Is JSON? Explained Simply for Beginners
Originally published on CodingCush. JSON, short for JavaScript Object Notation, is the standard way data travels between a website and a server. Every time a page loads your profile, a list of products, or the weather,
Originally published on CodingCush.
JSON, short for JavaScript Object Notation, is the standard way data travels between a website and a server. Every time a page loads your profile, a list of products, or the weather, the data behind it is almost always JSON. It looks like a JavaScript object, but it is really just text, and that is its superpower: any language can read it. In this guide you will learn what JSON is, its six simple syntax rules, how it differs from a JavaScript object, and how to read and write it with JSON.parse and JSON.stringify. Examples were run on Node.js v24.22.1 and Python 3.12.3.
What Is JSON, in Plain Words?
JSON is a text format for storing and moving structured data. It was built from JavaScript object syntax, but it is language independent, so Python, PHP, Java and every other language can read and write it. When your browser asks a server for a user profile, the server sends a small block of JSON text like this:
{
"name": "Meera",
"age": 22,
"skills": ["HTML", "CSS", "JavaScript"],
"active": true
}
That block says everything about one student in a form both humans and machines can read. The curly braces hold an object. Each line inside is a key and a value, separated by a colon. The values can be strings, numbers, arrays, booleans, null, or another object. Nothing else. That small set of types is the whole format, which is why JSON is easy to learn once you know the rules. If you are new to data moving between a browser and a server, our guide to what an API is, explained simply shows where JSON fits.
JSON Syntax: Six Rules to Remember
JSON has only six rules, and most errors come from breaking one of them. First, data is always in key value pairs, with the key in double quotes. Second, strings must use double quotes, never single quotes. Third, numbers are written plainly, with no quotes around them. Fourth, booleans are lowercase true or false. Fifth, an empty or unknown value is null. Sixth, there are no trailing commas after the last item, and no comments anywhere.
Take the student object from the introduction: the name is a double quoted string, the age is a bare number, active is a lowercase boolean, and the last item has no comma after it. The official JSON website at json.org shows the whole grammar as two small diagrams.
JSON vs JavaScript Objects: What Is Different?
JSON looks like a JavaScript object, and beginners often treat them as the same thing. They are not. A JavaScript object lives in memory and can hold functions, dates, undefined and single quoted strings. JSON is a text format with stricter rules, and it only carries data. This table shows the differences that actually bite:
| Feature | JavaScript object | JSON text |
|---|---|---|
| Keys | Quotes optional | Double quotes required |
| Strings | Single or double quotes | Double quotes only |
| Functions | Allowed | Not allowed |
| Comments | Allowed | Not allowed |
| Trailing comma | Allowed in modern JS | Not allowed, parse fails |
| undefined | Allowed | Not allowed, use null |
The practical rule is simple. Inside your program, use JavaScript objects freely. The moment data leaves your program, to a server, a file, or another language, it must become strict JSON text. JSON.parse and JSON.stringify guard that boundary.
Reading JSON in JavaScript: JSON.parse()
Data from a server arrives as a string, not as an object. JSON.parse() turns that string into a real JavaScript object:
const text = '{"name": "Meera", "age": 22, "skills": ["HTML", "CSS", "JavaScript"]}';
const student = JSON.parse(text);
console.log(student.name, student.skills[2]);
Output:
Meera JavaScript
Notice the quoting trick in the first line. The JSON text uses double quotes inside, so the JavaScript string around it uses single quotes. Once parsed, student is a normal object, and you read its properties with dot notation. If the JSON text is broken, JSON.parse() throws an error, so wrap it in try and catch in real code. Our guide to fetching data from an API in JavaScript shows where this parse step happens in a real request.
The JSON page in the MDN Web Docs, showing its static methods.
Writing JSON: JSON.stringify()
Going the other way, JSON.stringify() turns a JavaScript object into a JSON string, ready to send to a server or save to a file:
const user = { name: "Arjun", city: "Delhi", active: true };
console.log(JSON.stringify(user));
console.log(JSON.stringify(user, null, 2));
Output:
{"name":"Arjun","city":"Delhi","active":true}
{
"name": "Arjun",
"city": "Delhi",
"active": true
}
The first call produces compact JSON with no spaces, which is what you send over a network. The second call passes null and 2, which tells stringify to indent by two spaces, so the result is readable. The MDN page on the JSON object documents both methods and the extra options for filtering keys. Two things get dropped silently by stringify: functions and undefined values. If your object holds a function, it simply disappears from the JSON, so check your data before you send it.
A Real Example: Data from an API
Here is the whole round trip in one small example, the way it happens in real apps. A fake API response arrives as a JSON string, we parse it, and we pull out the fields we need:
const apiResponse = '{"id": 7, "title": "Learn JSON", "price": 0, "tags": ["web", "data"]}';
const course = JSON.parse(apiResponse);
const { title, tags } = course;
console.log(title, tags.join(", "));
Output:
Learn JSON web, data
The last two lines use destructuring to pull title and tags out of the parsed object in one move. If that syntax looks new, our guide to JavaScript destructuring explained with simple examples covers it from the beginning. This parse, then unpack pattern is the daily bread of front end work: fetch data, parse it, take the fields you need, and render them on the page. APIs that return something other than JSON exist, and our guide to what GraphQL is, explained simply shows one popular alternative and when teams choose it.
JSON in Other Languages: Python Does It Too
JSON is not a JavaScript only idea. Every major language has the same two functions under different names. Python uses json.loads() to parse and json.dumps() to stringify, and PHP has json_encode and json_decode doing the same job. That shared format is the reason JSON won: a JavaScript front end, a Python back end and a PHP script can all pass the same text around without arguing about the format. The Python round trip was tested on Python 3.12.3 and works exactly like the JavaScript one above.
Common Mistakes and Troubleshooting
Single quotes around strings. JSON demands double quotes. {'name': 'Meera'} is valid JavaScript but invalid JSON, and the parser will reject it. Use double quotes for every key and string value.
A trailing comma after the last item. {"name": "Meera",} fails with "Expected double-quoted property name". Delete the comma before the closing brace. This is the most common JSON error, and it comes from copying JavaScript habits into JSON.
Comments in JSON. JSON has no comments. A // note inside JSON text breaks the parse. Put your notes in the code around the JSON, never inside it.
undefined as a value. JSON has no undefined. Use null for a missing value, or leave the key out entirely.
Forgetting to parse, or parsing twice. If you call JSON.parse() on something that is already an object, it throws an error, because parse expects a string. Check whether your data is a string first, or log its type with typeof.
Frequently Asked Questions
What is JSON used for?
To store and move structured data between systems, most often between a web browser and a server, and between different programming languages.
Is JSON a programming language?
No. It is a text format for data, not a language. It has no variables, no loops and no logic, only the six syntax rules for objects, arrays and values.
What is the difference between JSON and XML?
Both carry structured data, but JSON is shorter, easier to read, and maps directly onto the data types of most languages. XML uses tags and is more verbose.
Can JSON contain functions or dates?
No. JSON holds only strings, numbers, booleans, null, arrays and objects. Dates are usually sent as strings, and functions must stay in your code, never in the JSON.
Why does JSON.parse fail with a SyntaxError?
The text is not valid JSON. The usual causes are single quotes, a trailing comma, or a comment. Paste it into a JSON validator, fix the first error it reports, and try again.
Conclusion
JSON is small, strict and everywhere. It is just text with six rules, but those six rules let a browser, a server and three different programming languages agree on the shape of data. Learn to read it, learn to parse it and stringify it, and the next time an API returns a wall of text, you will see a tidy object waiting to be unpacked. Open your console and parse the example from this guide.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.