JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



JS JSON

JavaScript Object Notation (JSON) 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 a text format that is completely language independent but uses conventions that are familiar to programmers of the C family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. JSON is often used to transmit data between a server and a web application, as an alternative to XML.

JSON is based on a subset of the JavaScript programming language, specifically the object literal notation of JavaScript. JSON is a collection of name/value pairs, where the name is a string and the value can be a string, number, boolean, null, array, or another JSON object. JSON objects are surrounded by curly braces {} and each name/value pair is separated by a comma. JSON arrays are surrounded by square brackets [] and each value is separated by a comma.

Brief Explanation of JS JSON

JSON is a lightweight data interchange format that is easy to read and write. It is often used to transmit data between a server and a web application, as an alternative to XML. JSON is based on a subset of the JavaScript programming language and is a collection of name/value pairs, where the name is a string and the value can be a string, number, boolean, null, array, or another JSON object. JSON objects are surrounded by curly braces {} and each name/value pair is separated by a comma. JSON arrays are surrounded by square brackets [] and each value is separated by a comma.

Code Examples

Here are some examples of JSON objects and arrays:


{
  "name": "John Smith",
  "age": 30,
  "isMarried": true,
  "hobbies": ["reading", "traveling", "photography"],
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "zip": "12345"
  }
}


[
  {
    "name": "John Smith",
    "age": 30,
    "isMarried": true,
    "hobbies": ["reading", "traveling", "photography"],
    "address": {
      "street": "123 Main St",
      "city": "Anytown",
      "state": "CA",
      "zip": "12345"
    }
  },
  {
    "name": "Jane Doe",
    "age": 25,
    "isMarried": false,
    "hobbies": ["painting", "hiking", "yoga"],
    "address": {
      "street": "456 Elm St",
      "city": "Othertown",
      "state": "NY",
      "zip": "67890"
    }
  }
]

JSON objects and arrays can be parsed and generated using JavaScript. Here is an example of parsing a JSON object:


var jsonString = '{"name": "John Smith", "age": 30, "isMarried": true}';
var jsonObject = JSON.parse(jsonString);
console.log(jsonObject.name); // outputs "John Smith"
console.log(jsonObject.age); // outputs 30
console.log(jsonObject.isMarried); // outputs true

And here is an example of generating a JSON object:


var jsonObject = {
  name: "John Smith",
  age: 30,
  isMarried: true
};
var jsonString = JSON.stringify(jsonObject);
console.log(jsonString); // outputs '{"name":"John Smith","age":30,"isMarried":true}'

References

Activity