JSON Array

Next ❯Stringify

Array is used as the value of an key inside JSON object

  • In JSON, array can store only valid JSON data types as value (string, number, array, object, boolean or null)
  • turned_in_notArray can be divided in two ways
    • Simple Array
    • Nested Array


Simple Array
[value1,value2,value3,....,valueN]

Example

[75,65,89,95]

In json just put key value as an array

Example

{"class":6,"marks":[75,65,89,95]}

Here above, keyName "marks" is having an array as its value

Hope you remember how to access an object value, if not then check JSON Object

Now, think above example as:

var obj = {"class":6,"marks":someValue};
So, to access keyName "marks" value we have 2 options
obj.marks;
OR
obj["marks"];
  • Remember! if you use the above syntax for an array then it will print all the values declared inside an array (but object value inside an array print as object)

Now, you can access specific array value via its index

obj.marks[index];
OR
obj["marks"][index];

*using dot (.)


Syntax: obj.keyName[index]

var obj = {"class":6,"marks":[75,65,89,95]};
$("p").html(obj.marks[2]);
Get Valuesubject
Get Valueclose
<!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 = {"class":6,"marks":[75,65,89,95]};
  $("p").html(obj.marks[2]);
</script>
</body>
</html>

aaa

*using bracket ([ ])


Syntax: obj.["keyName"][index]

var obj = {"class":6,"marks":[75,65,89,95]};
$("p").html(obj["marks"][1]);
Get Valuesubject
Get Valueclose
<!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 = {"class":6,"marks":[75,65,89,95]};
  $("p").html(obj["marks"][1]);
</script>
</body>
</html>

/*Syntax-To change an array index value*/

obj.keyName[index]=newValue;
OR
obj["keyName"][index]=newValue;

var obj = {"class":6,"marks":[75,65,89,95]};
$("p").html("Before : "+ obj.marks[1]);
obj.marks[1]=99;
$("p").append("&lt;br>After Changing : "+ obj.marks[1]);
Change Valuesubject
Change Valueclose
<!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 = {"class":6,"marks":[75,65,89,95]};
  $("p").html("Before : "+ obj.marks[1]);
  obj.marks[1]=99;
  $("p").append("<br/>After Changing : "+ obj.marks[1]);
</script>
</body>
</html>

/*Syntax-To delete an array index value*/

delete obj.keyName[index];
OR
delete obj["keyName"][index];

var obj = {"class":6,"marks":[75,65,89,95]};
$("p").html("Before : "+ obj.marks[1]);
delete obj.marks[1];
$("p").append("<br>After deleting : "+ obj.marks[1]);
Delete Valuesubject
Delete Valueclose
<!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 = {"class":6,"marks":[75,65,89,95]};
  $("p").html("Before : "+ obj.marks[1]);
  delete obj.marks[1];
  $("p").append("<br/>After deleting : "+ obj.marks[1]);
</script>
</body>
</html>



Nested Array

Array inside an array

Example

[0,[1,2,3],4,5]
In Json
{ "number":[0,[1,2,3],4,5] }

Here, above keyName "number" is having nested array



var obj = { "number":[0,[1,2,3],4,5] };
$("p").html("index[1] : "+ obj.number[1]+
    "<br> index[1][1] : "+ obj.number[1][1]);
Nested Arraysubject
Nested Arrayclose
<!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 = { "number":[0,[1,2,3],4,5] };
  $("p").html("index[1] : "+ obj.number[1]+
    "<br> index[1][1] : "+ obj.number[1][1]);
</script>
</body>
</html>

To access more inside nested array

/*Syntax-In general way*/

obj.keyName[index][nextIndex1]...[nextIndexN];
OR
obj.["keyName"][index][nextIndex1]...[nextIndexN];

To change nested array value

/*Syntax-In general way*/

obj.keyName[index][nextIndex1]...[nextIndexN]= newValue;
OR
obj.["keyName"][index][nextIndex1]...[nextIndexN]= newValue;

To delete nested array value

/*Syntax-In general way*/

delete obj.keyName[index][nextIndex1]...[nextIndexN];
OR
delete obj.["keyName"][index][nextIndex1]...[nextIndexN];

  • Stringify
❮ Prev Objects
Next ❯Stringify
TryOut Examples"Learn to Explore..!"

TryOut Editor

receipt