IP Lecture 18 JSON
IP Lecture 18 JSON
Content
• Introduction
• Why to use JSON?
• JSON Syntax Rules
• JSON.parse
• JSON.stringify
Introduction
• JSON stands for JavaScript Object Notation
o A text format for storing and transporting data
If you parse the JSON string with a JavaScript program, you can access the
data as an object:
Built-in functions:
• JSON.parse(): For converting JSON strings into JavaScript objects
Example
"name":"John"
JSON Syntax Rules
JSON - Evaluates to JavaScript Objects
{"name":"John"}
{"age":30}
}
JSON Syntax Rules
JSON Values
{"sale":true}
{"middlename":null}
JSON Syntax Rules
JSON Values
Use the JavaScript function JSON.parse() to convert text into a JavaScript object:
Make sure the text is in JSON format, or else you will get a syntax error.
<p id="demo"></p>
<script>
const txt = '{"name":"John", "age":30, "city":"New York"}'
const obj = JSON.parse(txt);
document.getElementById("demo").innerHTML = obj.name + ", " +
obj.age;
</script>
</body>
</html>
Arrays as JSON
<!DOCTYPE html>
<html> • When using the JSON.parse()
<body> on a JSON derived from an
<h2>Parsing a JSON Array.</h2> array, the method will return
<p>Data written as an JSON array will be parsed into a
JavaScript array.</p> a JavaScript array, instead of
<p id="demo"></p> a JavaScript object.
<script>
const text = '[ "Ford", "BMW", "Audi", "Fiat" ]';
const myArr = JSON.parse(text);
document.getElementById("demo").innerHTML = myArr[0];
</script>
</body>
</html>
Exceptions
<!DOCTYPE html> Parsing Dates
<html>
<body> • Date objects are not
<h2>Convert a string into a date object.</h2>
<p id="demo"></p> allowed in JSON.
<script> • If you need to include a
const text = '{"name":"John", "birth":"1986-12-14", "city":"New
York"}'; date, write it as a string.
const obj = JSON.parse(text);
obj.birth = new Date(obj.birth);
document.getElementById("demo").innerHTML = obj.name + ", " + • You can convert it back
obj.birth;
</script> into a date object later
</body>
</html>
Exceptions
<!DOCTYPE html>
<html> Parsing Dates
<body>
• you can use the second
<h2>Convert a string into a date object.</h2>
parameter, of the
<p id="demo"></p>
JSON.parse() function, called
<script> reviver.
const text = '{"name":"John", "birth":"1986-12-14", "city":"New
York"}'; • The reviver parameter is a
const obj = JSON.parse(text, function (key, value) {
if (key == "birth") { function that checks each
return new Date(value);
} else { property, before returning the
return value;
} value.
});
document.getElementById("demo").innerHTML = obj.name + ", " +
obj.birth;
</script>
</body>
</html>
<!DOCTYPE html> Exceptions
<html>
<body> Parsing Functions
<script>
const obj = {name: "John", age: 30, city: "New York"};
const myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
</script>
</body>
</html>
Stringify JavaScript Array
<!DOCTYPE html> • It is also possible to stringify JavaScript
<html>
<body> arrays
<h2>Create a JSON string from a JavaScript • Imagine we have this array in JavaScript:
array.</h2>
<p id="demo"></p> const arr = ["John", "Peter", "Sally",
"Jane"];
<script>
const arr = ["John", "Peter", "Sally", "Jane"]; • Use the JavaScript function JSON.stringify()
const myJSON = JSON.stringify(arr);
document.getElementById("demo").innerHTML = to convert it into a string.
myJSON;
</script> const myJSON = JSON.stringify(arr);
</body>
</html>
Exception
<!DOCTYPE html>
<html> Stringify Dates
<body>
• In JSON, date objects are not
<h2>JSON.stringify() converts date objects into allowed.
strings.</h2>
<p id="demo"></p> • The JSON.stringify() function will
convert any dates into strings.
<script>
const obj = {name: "John", today: new Date(), city: • You can convert the string back
"New York"};
const myJSON = JSON.stringify(obj); into a date object at the receiver.
document.getElementById("demo").innerHTML = myJSON;
</script>
</body>
</html>
Exception
Stringify Functions
<!DOCTYPE html>
<html> • In JSON, functions are not allowed as
<body>
object values.
<h2>JSON.stringify() will remove any functions • The JSON.stringify() function will
from an object.</h2>
<p id="demo"></p> remove any functions from a
</body>
</html>
Exception
<!DOCTYPE html>
<html>
<body> Stringify Functions
<h2>JSON.stringify() will remove any functions from an
• In JSON, functions are not allowed as
object.</h2> object values.
<p>Convert functions into strings to keep them in the JSON
object.</p> • The JSON.stringify() function will
<p id="demo"></p> remove any functions from a JavaScript