Successfully reported this slideshow.
Your SlideShare is downloading. ×

Working with JSON.pptx

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
Intro to JSON
Intro to JSON
Loading in …3
×

Check these out next

1 of 17 Ad

Working with JSON.pptx

Download to read offline

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa). You'll come across it quite often, so in this article, we give you all you need to work with JSON using JavaScript, including parsing JSON so you can access data within it, and create JSON.

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa). You'll come across it quite often, so in this article, we give you all you need to work with JSON using JavaScript, including parsing JSON so you can access data within it, and create JSON.

Advertisement
Advertisement

More Related Content

More from Chitkara University (20)

Advertisement

Recently uploaded (20)

Working with JSON.pptx

  1. 1. Unit-2 Working with JSON
  2. 2. What is JSON? J ava S cript O bject N otation, or JSON, is a lightweight data format that has become the defacto standard for the web. JSON can be represented as either a list of values, e.g. an Array, or a hash of properties and values, e.g. an Object. Array, Object [ a, m, y ] { name : value }
  3. 3. // a JSON array ["one", "two", "three"] // a JSON object { "one": 1, // name : “Mukesh” "two": 2, "three": 3 } Format of Node.js Object: JSON- object • Create an Object of student: var student = { Name: "Amit", Branch:"BCA", City: "kathmandu", Mobile: "99946730" };
  4. 4. Encoding and Decoding JavaScript provides 2 methods for encoding data structures to json and encoding json back to JavaScript objects and arrays. They are both available on the JSON object that is available in the global scope. JSON.stringify takes a JavaScript object or array and returns a serialized string in the JSON format.
  5. 5. const data = { name: 'John Doe', age: 32, title: 'Vice President of JavaScript' }; const jsonStr = JSON.stringify(data); console.log(jsonStr); // prints '{"name":"John Doe","age":32,"title":"Vice President of JavaScript"}'
  6. 6. JSON.parse takes a JSON string and decodes it to a JavaScript data structure. const jsonStr = '{"name":"John Doe","age":32,"title":"Vice President of JavaScript"}'; const data = JSON.parse(jsonStr); console.log(data.title); // prints 'Vice President of JavaScript'
  7. 7. What is valid JSON? There are a few rules to remember when dealing with data in JSON format. There are several gotchas that can produce invalid JSON as well. • Empty objects and arrays are okay • Strings can contain any unicode character, this includes object properties • null is a valid JSON value on it's own • All object properties should always be double quoted • Object property values must be one of the following: String, Number, Boolean, Object, Array, null • Number values must be in decimal format, no octal or hex representations • Trailing commas on arrays are not allowed
  8. 8. These are all examples of valid JSON. {"name":"John Doe","age":32,"title":"Vice President of JavaScript"} ["one", "two", "three"] // nesting valid values is okay {"names": ["John Doe", "Jane Doe"] } [ { "name": "John Doe"}, {"name": "Jane Doe"} ] {} // empty hash [] // empty list null { "key": "uFDD0" } // unicode escape codes
  9. 9. These are all examples of bad JSON formatting. { name: "John Doe", 'age': 32 } // name and age should be in double quotes [32, 64, 128, 0xFFF] // hex numbers are not allowed { "name": "John Doe", "age": undefined } // undefined is an invalid value // functions and dates are not allowed { "name": "John Doe", "birthday": new Date('Fri, 26 Jan 2019 07:13:10 GMT'), "getName": function() { return this.name; }}
  10. 10. Calling JSON.parse with an invalid JSON string will result in a SyntaxError being thrown. If you are not sure of the validity of your JSON data, you can anticipate errors by wrapping the call in a try/catch block. Notice that the only complex values allowed in JSON are objects and arrays. Functions, dates and other types are excluded. This may not seem to make sense at first. But remember that JSON is a data format, not a format for transferring complex JavaScript objects along with their functionality.
  11. 11. Example-1 const book = { title : “Node.js Book", author : "Basarat Ali Syed" } const bookJSON = JSON.stringify(book) console.log(book) console.log(bookJSON)
  12. 12. Example-2 const book = { title : "Narinder Modi", author : "xyz" } const bookJSON = JSON.stringify(book) console.log(book.title) console.log(bookJSON.title)
  13. 13. Example-3 const book = { title : "Narinder Modi", author : "xyz" } const bookJSON = JSON.stringify(book) console.log(book.title) console.log(bookJSON.title) const bookObj = JSON.parse(bookJSON) console.log(bookObj.title)
  14. 14. Example-4 const fs = require("fs") const book = { title : "Narinder Modi", author : "xyz" } const bookJSON = JSON.stringify(book) console.log(book.title) console.log(bookJSON.title) const bookObj = JSON.parse(bookJSON) console.log(bookObj.title) fs.writeFileSync("myjson.json",bookJSON)
  15. 15. Example-5 const fs = require("fs") //const book = { // title : "Narinder Modi", // author : "xyz" //} //const bookJSON = JSON.stringify(book) //console.log(book.title) //console.log(bookJSON.title) //const bookObj = JSON.parse(bookJSON) const dataBuffer = fs.readFileSync("myjson.json") //console.log(dataBuffer) console.log(dataBuffer.toString())
  16. 16. Example-6 const fs = require("fs") //const book = { // title : "Narinder Modi", // author : "xyz" //} //const bookJSON = JSON.stringify(book) //console.log(book.title) //console.log(bookJSON.title) //const bookObj = JSON.parse(bookJSON) const dataBuffer = fs.readFileSync("myjson.json") //console.log(dataBuffer) const dataJSON = dataBuffer.toString() const bookObj = JSON.parse(dataJSON) console.log(bookObj)
  17. 17. Example-7 const fs = require("fs") //const book = { // title : "Narinder Modi", // author : "xyz" //} //const bookJSON = JSON.stringify(book) //console.log(book.title) //console.log(bookJSON.title) //const bookObj = JSON.parse(bookJSON) const dataBuffer = fs.readFileSync("myjson.json") //console.log(dataBuffer) const dataJSON = dataBuffer.toString() const bookObj = JSON.parse(dataJSON) console.log(bookObj.title)

×