JSON
JSON
JSON
What is JSON?
● JSON stands for JavaScript Object Notation.
● JSON is a lightweight data-interchange format.
● JSON is a plain text written in JavaScript object notation.
● JSON is a language independent.
● JSON is a text-based data format that is used to store and transfer
data.
● It is a standard text-based format for represent structured data based
on JavaScript object syntax and commonly used for transmitting data in
web applications.
● For example: sending some data from the server to the client, so it can
be displayed on a web page, or vice versa.
JSON Example
● This example is a JSON string:
‘ {“name”:”John”, “age”:30, “car”:null}
’
● It defines an object with 3 properties: name, age, car.
● Each property has a value.
● If we parse the JSON string with a JavaScript program, we can
access the data as an object:
{
“employee”:[ “Hakim”, “Rafiq”, “Khadeeja”]
}
JSON Data Types
{ “sale”: true }
● JSON Booleans: values can be true/false
{ “middlename”:null }
● JSON null: values can be null
JSON.parse( )
● A common use of JSON is to exchange data to/from a web server
● When receiving data from a web server, the data is always a string
● Parse the data with JSON.parse(), and the data becomes a JS object
● Example - Parsing JSON
○ Imagine we received this text from a web server:
‘ { “name”: “Hakim”, “age”: 30, “city”: “Bangi”} ’
*avoid using functions in JSON as they will lose their scope and we have to use
eval() to convert them back into functions.
JSON.stringify()
● A common use of JSON is to exchange data to/from a web server.
● When sending data to a web server, the data has to be a string.
● JS object can be converted into a string with JSON.stringify().
Stringify a JavaScript Object
● Let say we have this object in JavaScript:
const obj = { name: “Hakim”, age: 30, city:
“Bangi” } ;
● Use the JavaScript function JSON.stringify() to convert it into a
string.
const myJSON = JSON.stringify(obj) ;
myArray[0];
● Example:
{
“name”:“Hakim”,
“age”:25,
“car”:[“Ford”, “BMW”, “”Fiat”]
}
● The fetch() method returns a Promise so you can use the then() and
catch() methods to handle it:
● When the request completes, the resource is available. At this time,
the promise will resolve into a Response object.
● The Response object is the API wrapper for the fetched resource. The
Response object has a number of useful properties and methods to
inspect the response.
Reading the Response
● If the contents of the response are in the raw text format, you can
use the text() method. The text() method returns a Promise that
resolves with the complete contents of the fetched resource:
● In practice, you often use the async/await with the fetch() method
like this:
JavaScript Fetch API example
● Suppose that you have a json ● The following shows the HTML
file that locates on the page:
webserver with the following
contents:
● In the app.js, we’ll use the fetch() method to get the user data
and render the data inside the <div> element with the class
container.
● First, declare the getUsers() function that fetches users.json from
the server.
● Then, develop the renderUsers() function that renders user data: