Table of Contents
JSON is a lightweight data interchange format. Here is a JSON example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript JSON vs XML</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>JSON Example</h4>
<p>JSON data:</p>
<pre id="demo"></pre>
<script>
// JSON object representing a person
const person = {
"firstName": "John",
"lastName": "Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "New York"
},
"phoneNumbers": ["555-1234", "555-5678"]
};
document.getElementById("demo").innerHTML = JSON.stringify(person, null, 2);
</script>
</body>
</html>

Result View Example
XML (Extensible Markup Language) is another format for storing and transporting data:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript JSON vs XML</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>XML Example</h4>
<p>XML data displayed in HTML:</p>
<pre id="demo" style="color:blue"></pre>
<script>
// Simulating XML-like data structure
const xmlData =
"<person>\n" +
" <firstName>John</firstName>\n" +
" <lastName>Doe</lastName>\n" +
" <age>30</age>\n" +
" <address>\n" +
" <street>123 Main St</street>\n" +
" <city>New York</city>\n" +
" </address>\n" +
" <phoneNumbers>\n" +
" <number>555-1234</number>\n" +
" <number>555-5678</number>\n" +
" </phoneNumbers>\n" +
"</person>";
// Use textContent to display raw XML without parsing HTML tags
document.getElementById("demo").textContent = xmlData;
</script>
</body>
</html>

Result View Example
JSON uses objects (key/value pairs) and arrays. XML uses elements with opening and closing tags:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript JSON vs XML</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>JSON Uses Objects and Arrays</h4>
<p id="demo"></p>
<script>
// JSON: employees array with objects
const employees = {
"employees": [
{ "name": "John", "department": "Sales" },
{ "name": "Jane", "department": "Marketing" },
{ "name": "Bob", "department": "IT" }
]
};
let text = "JSON Employees:<br>";
for (let emp of employees.employees) {
text += emp.name + " - " + emp.department + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>

Result View Example
JSON integrates naturally with JavaScript since it shares the same syntax:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript JSON vs XML</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Working with JSON in JavaScript</h4>
<p id="demo"></p>
<script>
// JSON data as string
const jsonString = '{"name":"John","age":30,"city":"New York"}';
// Parse JSON to JavaScript object
const obj = JSON.parse(jsonString);
// Easily access properties
document.getElementById("demo").innerHTML =
"Name: " + obj.name + "<br>" +
"Age: " + obj.age + "<br>" +
"City: " + obj.city;
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.