JSON_Session
JSON_Session
var student = {
“age”: 26,
“Hobbies”: [
“Reading”,
“Acting”,
“Gardening”
],
“name”: “Aswathy”,
“Qualification”:”Btech”,
“Sex”: “Female”
}
student.age=20
student
var student = {
“age”: 20,
“Hobbies”: [
“Reading”,
“Acting”,
“Gardening”
]
“name”: “Aswathy”,
“Qualification”:”Btech”,
“Sex”: “Female”
}
delete (student.Sex)
return true
student
var student = {
“age”: 20,
“Hobbies”: [
“Reading”,
“Acting”,
“Gardening”
],
“name”: “Aswathy”,
“Qualification”:”Btech”
}
In console window js is actually processed.
delete(student.Hobbies[0])
return true
student
index 0th value will get deleted, but length : 3. So while working with array delete
doesn’t work well. So we can use splice in such cases.
var student = {
“age”: 20,
“Hobbies”: [
“Acting”,
“Gardening”
]
“name”: “Aswathy”,
“Qualification”:”Btech”
}
Splice
student.Hobbies.splice(0,1)
Return undefined 0 , because 0th index is already deleted.
Student.Hobbies.splice(1,1)
It will remove 1st index i.e; value Gardening from the Hobbies Array.
In hobbies, 0: Acting & length : 1
student
var student = {
“age”: 20,
“Hobbies”: [
“Acting”
]
“name”: “Aswathy”,
“Qualification”:”Btech”
}
If we apply a debug point, it will stop there.
We can check if any variable has executing property or not.
var student = {
“age”: 20,
“Hobbies”: [
“Acting”
],
“name”: “Aswathy”,
“Qualification”:”Btech”,
“subject”:[
{AI: “Artificial Intelligence”},
{ML:”Machine Learning”}
]
}
student.subject
we get an array(2),
0: Object
AI : Artificial Intelligence
1:Object
ML: Machine Learning
Length: 2
for(var i=0;i<=student.subject.length;i++){
for(key in student.subject[i]){
console.log(key);
}
}
Output : AI, ML
Let’s say we have JSON data in employees.json file. Inorder to fetch this json data & show it in
div’s in index.html file, we can do it using Ajax or Jquery.
Fetching JSON data using AJAX(Asynchronous Javascript And XML)
Script.js
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if(xhr.status === 200){
}
}
};
xhr.send();
Index.html
<div> id=”employees1”</div>
Fetching JSON data using jQuery(Simplifies AJAX call & DOM manipulation)
JQUERY is a Javascript Library.
$(document).ready(function(){
$.getJSON(‘employees.json’,function(data){
var html2=’<ul>’;
$.each(data, function(index,employee){
html2=’<ul>’ + employee.name + ‘<ul>’;
});
html2+=’</ul>’;
document.getElementById(‘employees2’).innerHTML=html2;
});
});
Index.html
<div id=”employees2></div>
Add all js library and js script tags below your content.
Also make sure all frameworks or external libraries like jquery is added 1 st, then only
add script tag for js files. Otherwise data won’t be visible in view.
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if(xhr.status === 200){
var data = JSON.parse(xhr.responseText);
console.log(data);
}
}
}
}
Example 1
<p>Access a JSON object using bracket notation:</p>
<p id="Sample"></p>
<script>
var myObj, x;
myObj = { "name":"Arun", "age":28, "Company":"TCS"};
x = myObj["Company"];
document.getElementById("Sample").innerHTML = x;
</script>
OR
Example 2
Looping an Object
for-in loop can be used to loop in object properties.
Example
Example:
for (x in myObj) {
document.getElementById("sample").innerHTML += myObj[x] + "<br>";
}
module.exports= function(){
json.forEach(function(element, index){
element['aggregate'] = parseInt(element['aggregate']);
element.forEach(function(per,ind){
per['sub1'] = parseInt(per['sub1']);
per['sub2'] = parseInt(per['sub2']);
per['sub3'] = parseInt(per['sub3']);
}); });
return json;
}
Accessing an Array
Example
<p>Access an array value of a JSON object</p>
<p id="Sample"></p>
<script>
var myObj, x;
myObj = {
"name":"Arun",
"age":29,
"cars":["Hyundai", "Volkswagen", "Maruti Suzuki"]
};
x = myObj.cars[0];
document.getElementById("Sample").innerHTML = x;
</script>
Nested Array
In Nested arrays, to access arrays inside arrays, use a for-in loop for each
array.
Example
myObj = {
"name":"Arun",
"age":28,
"cars": [
{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
{ "name":"BMW", "models":[ "320", "X3", "X5" ] },
{ "name":"Fiat", "models":[ "500", "Panda" ] }
]
}
for (i in myObj.cars) {
x += "<h2>" + myObj.cars[i].name + "</h2>";
for (j in myObj.cars[i].models) {
x += myObj.cars[i].models[j] + "<br>";
}
}
document.getElementById("Sample").innerHTML = x;
</script>
delete myObj.cars[1];
for (i in myObj.cars) {
x += myObj.cars[i] + "<br>";
}
document.getElementById("Sample").innerHTML = x;
</script>
"persons":{
"Name":"Gayathri",
"EmployeeID": 12321,
"Experience" : 2,
"Company": "TCS",
[
{
"Name":"Pizza",
"Type":"Food",
"Price":300
},
{
"Name":"Burger",
"Type":"Food1",
"Price":200
},
{
"Name":"Sandwich",
"Type":"Food2",
"Price":250
}
]
Example
{
"Monday": {
"description": "sunny",
"maxTemp": 22,
"minTemp": 20,
"windSpeed": 12,
"danger": false
}
}
{
"forecast": [
{
"Monday": {
"description": "sunny",
"maxTemp": 22,
"minTemp": 20,
"windSpeed": 12,
"danger": false
}
},
{
"Tuesday": {
"description": "windy",
"maxTemp": 22,
"minTemp": 20,
"windSpeed": 40,
"danger": true
}
},
{
"Wednesday": null
}
]
}