JSON Objects
- In JSON, object can store only valid JSON data types as value (string, number, array, object, boolean or null)
- turned_in_notObjects can be divided in two ways
- Simple Object
- Nested Object
Simple Object
{ "key1":value1, "key2":value2,...., "keyN":valueN }
Example
{ "name":"Riya", "age":23 }
/*To access the object single/direct value*/
Riya
*using dot (.)
Syntax: obj.keyName
var obj = {"name":"Riya","age":23};
$("p").html(obj.name);
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
<!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var obj = { "name":"Riya", "age":23 };
$("p").html(obj.name);
</script>
</body>
</html>
Riya
*using bracket ([ ])
Syntax: obj["keyName"]
var obj = {"name":"Riya","age":23};
$("p").html(obj["name"]);
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
<!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var obj = { "name":"Riya", "age":23 };
$("p").html(obj["name"]);
</script>
</body>
</html>
/*To access the object all Keys & values*/
Key:name, Value:Riya
Key:age, Value:23
var obj = {"name":"Riya","age":23};
for(var key in obj){
$("p").append("Key:"+key+", Value:"+obj[key]+"<br>");
}
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
<!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var obj = {"name":"Riya","age":23};
for(var key in obj){
$("p").append("Key:"+key+", Value:"+obj[key]+"<br>");
}
</script>
</body>
</html>
/*Syntax-To change the object value*/
obj.keyName=newValue;
ORobj["keyName"]=newValue;
Before : Riya
After Changing : Rahul
var obj = {"name":"Riya","age":23};
$("p").html("Before : "+ obj.name);
obj.name="Rahul";
$("p").append("<br>After Changing : "+ obj.name);
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
<!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var obj = { "name":"Riya", "age":23 };
$("p").html("Before : "+ obj.name);
obj.name="Rahul";
$("p").append("<br>After Changing : "+ obj.name);
</script>
</body>
</html>
/*Syntax-To delete the object value*/
delete obj.keyName;
ORdelete obj["keyName"];
Before : Riya
After deleting : undefined
var obj = {"name":"Riya","age":23};
$("p").html("Before : "+ obj["name"]);
delete obj["name"];
$("p").append("<br>After deleting : "+ obj["name"]);
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
<!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var obj = { "name":"Riya", "age":23 };
$("p").html("Before : "+ obj["name"]);
delete obj["name"];
$("p").append("<br>After deleting : "+ obj["name"]);
</script>
</body>
</html>
Nested Object
Object inside object
Example
{
"phone":{ "modal":"iphone X", "color":"black", "price":"$999" },
"brand":"apple"
}
Here, above keyName "phone" is having another object , you can refer it (phone) as a keyObject for better understanding
/*Syntax-To access nested object single key*/
obj.keyObject.keyName;
ORobj.keyObject["keyName"];
Your phone modal is : iphone X
and color is : black
var obj = {
"phone":{ "modal":"iphone X", "color":"black", "price":"$999" },
"brand":"apple"
};
$("p").html("Your phone modal is : "+ obj.phone["modal"]+
"<br> and color is : "+ obj.phone.color);
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
<!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var obj = { "phone":{
"modal":"iphone X", "color":"black", "price":"$999" },
"brand":"apple"
};
$("p").html("Your phone modal is : "+ obj.phone["modal"]+
"<br> and color is : "+ obj.phone.color);
</script>
</body>
</html>
To access more inside nested objects
/*Syntax-In general way*/
obj.keyObject.nextKeyObject1.nextKeyObject2....nextKeyObjectN.keyName;
ORobj.keyObject.nextKeyObject1.nextKeyObject2....nextKeyObjectN["keyName"];
To change nested object value
/*Syntax-In general way*/
obj.keyObject.nextKeyObject1.nextKeyObject2....nextKeyObjectN.keyName= newValue;
ORobj.keyObject.nextKeyObject1.nextKeyObject2....nextKeyObjectN["keyName"]= newValue;
To delete nested object value
/*Syntax-In general way*/
delete obj.keyObject.nextKeyObject1.nextKeyObject2....nextKeyObjectN.keyName;
ORdelete obj.keyObject.nextKeyObject1.nextKeyObject2....nextKeyObjectN["keyName"];