Skip to content

Latest commit

 

History

History
114 lines (87 loc) · 4.76 KB

41-JSON-XML.md

File metadata and controls

114 lines (87 loc) · 4.76 KB

JSON

JSON stands for JavaScript Object Notation. It is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. JSON is often used to transmit data between a server and a web application, as an alternative to XML.

JSON data is represented as a collection of key-value pairs, similar to an object in JavaScript. The keys are always strings, and the values can be any valid JSON data type, including strings, numbers, objects, arrays, and booleans. Here's an example of a simple JSON object:

{
  "name": "John Smith",
  "age": 30,
  "isStudent": true,
  "favoriteFoods": ["pizza", "tacos", "sushi"]
}

In JavaScript, you can parse a JSON string into a JavaScript object using the JSON.parse() method, like this:

const jsonString = '{"name": "John Smith", "age": 30, "isStudent": true, "favoriteFoods": ["pizza", "tacos", "sushi"]}';
const jsonObj = JSON.parse(jsonString);
console.log(jsonObj.name); // output: John Smith

You can also convert a JavaScript object into a JSON string using the JSON.stringify() method, like this:

const obj = { name: "John Smith", age: 30, isStudent: true, favoriteFoods: ["pizza", "tacos", "sushi"] };
const jsonString = JSON.stringify(obj);
console.log(jsonString); // output: {"name":"John Smith","age":30,"isStudent":true,"favoriteFoods":["pizza","tacos","sushi"]}

JSON is commonly used in JavaScript for exchanging data between a client and a server, as well as for storing and retrieving data in web applications. Many APIs return data in JSON format, making it easy to work with in JavaScript. JSON is also used in NoSQL databases, such as MongoDB, to store data in a document-oriented format.

XML

XML stands for eXtensible Markup Language. It is a markup language that is similar to HTML but is designed to store and transport data, rather than to display it. XML is often used as a standard format for exchanging data between different systems and applications.

XML data is represented as a collection of elements, similar to HTML. Each element has a start tag, an end tag, and can contain attributes and nested elements. Here's an example of a simple XML document:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="Science Fiction">
    <title>The Hitchhiker's Guide to the Galaxy</title>
    <author>Douglas Adams</author>
    <price>9.99</price>
  </book>
  <book category="Mystery">
    <title>The Da Vinci Code</title>
    <author>Dan Brown</author>
    <price>15.99</price>
  </book>
</bookstore>

In JavaScript, you can parse an XML string into a JavaScript object using the DOMParser object, like this:

const xmlString = `<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="Science Fiction">
    <title>The Hitchhiker's Guide to the Galaxy</title>
    <author>Douglas Adams</author>
    <price>9.99</price>
  </book>
  <book category="Mystery">
    <title>The Da Vinci Code</title>
    <author>Dan Brown</author>
    <price>15.99</price>
  </book>
</bookstore>`;

const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "text/xml");
console.log(xmlDoc.getElementsByTagName("book")[0].getAttribute("category")); // output: Science Fiction

You can also convert a JavaScript object into an XML string using the XMLSerializer object, like this:

const books = [
  {category: "Science Fiction", title: "The Hitchhiker's Guide to the Galaxy", author: "Douglas Adams", price: 9.99},
  {category: "Mystery", title: "The Da Vinci Code", author: "Dan Brown", price: 15.99}
];

const xmlDoc = document.createElement("bookstore");

books.forEach(book => {
  const bookElem = document.createElement("book");
  bookElem.setAttribute("category", book.category);
  
  const titleElem = document.createElement("title");
  titleElem.textContent = book.title;
  
  const authorElem = document.createElement("author");
  authorElem.textContent = book.author;
  
  const priceElem = document.createElement("price");
  priceElem.textContent = book.price;
  
  bookElem.appendChild(titleElem);
  bookElem.appendChild(authorElem);
  bookElem.appendChild(priceElem);
  
  xmlDoc.appendChild(bookElem);
});

const serializer = new XMLSerializer();
const xmlString = serializer.serializeToString(xmlDoc);
console.log(xmlString);

XML is commonly used in JavaScript for exchanging data between a client and a server, as well as for storing and retrieving data in web applications. It is often used in SOAP web services, which use XML messages to exchange information between different systems. However, JSON has become more popular in recent years due to its simplicity and smaller file sizes.