Updated April 14, 2023
Introduction to JSON Array of Strings
Here is one more add on to the knowledge meter for your development. It is the topic we are got to be exploring. So let’s break the name here to understand better. JSON is JavaScript Object Notation is used for data interchange, Array of strings is an ordered list of values with string type. So on a whole, the ‘JSON array of strings’ represents an ordered list of values, and It can store multiple values. This is useful to store string, Boolean, number, or an object. Here as we are particularly focusing on an Array of Strings, so let us deep dive with its syntax and few examples.
Syntax:
["value1", "value2", "value3",……..]
For Example:
["January", "February", "March", "April", "May", "June", "July"]
These square brackets are used to declare JSON array [ ], JSON as they can store multiple values and value types, these values must be separated by ‘,’ comma. Arrays in JSON are very much similar to arrays in JavaScript.
{
"empName" : "Karthick",
"empage" : 42,
"empRights" : [ "Engineer", "SupportService", "Devloper" ]
}
So here empRights is an array of strings enclosed inside square brackets with quotations.
Examples of JSON Array of Strings
Following are the examples as given below:
Example #1
Code:
<!DOCTYPE html>
<html>
<body>
<p>Looping inside an array using for loop</p>
<p id="demo"></p>
<script>
var sample, i, x = "";
sample = {
"empName" : "Karthick",
"empage" : 42,
"empRights" : [ "Engineer", "SupportService", "Devloper" ]
};
for (i in sample.empRights) {
x += sample.empRights[i] + "<br>";
}
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Output:
Example #2
Code:
<!DOCTYPE html>
<html>
<body>
<p>Looping inside an array using for loop also retrieving value using index</p>
<p id="demo"></p>
<script>
var sample, i, x = "";
sample = {
months: ["January", "February", "March", "April", "May", "June", "July"]
};
for (i in sample.months) {
x += sample.months[i] + "<br>";
}
document.getElementById("demo").innerHTML = x;
document.write('4th month in a year is ',sample.months[3]);
</script>
</body>
</html>
Output:
How JSON Array of String Works?
Basically a valid JSON always starts with either curly braces else square brackets. { } OR [ ]. So
{ will start an object
{ string : value },
{"key":"value", ….}
JSON takes double quotations whereas JavaScript takes single quotations.
[ will start array and ] will end the array
[ value ],
Values can be an object, an array, Boolean, string, digits or NULL.
JSON array of numbers:
[90, 78, 56, 34, 21]
JSON array of Booleans:
[true, true, false, true, false]
JSON array of Objects:
{"CGemployees":[
{"empname ":"Karthick", "empemail":"[email protected]", "age":73},
{"empname ":"Shyam", "empeemail":"[email protected]", "age":88},
{"empname ":"Saideep", "empemail":"[email protected]", "age":33},
{"empname ":"Anusha", "empemail":"[email protected]", "age":21}
]}
We can also store multi dimensional array of strings,
[
[ "abc", "bcd", "cda" ],
[ "mno", "nop", "opq" ],
[ "xyz", "yza", "zab" ]
]
Example #3
Code:
<!DOCTYPE html>
<html>
<body>
<p>Looping multi dimensional array</p>
<p id="demo"></p>
<script>
var sample, i, j, x = "";
var sample = {
"name" : "Karthick",
"alphabets" : [
[ "abc", "def", "ghi" ],
[ "jkl", "mno", "pqr" ],
[ "stu", "vwx", "yz" ]
]
}
for (i in sample.alphabets) {
for (j in sample.alphabets[i]) {
x = sample.alphabets[i][j];
document.write(x);
}
}
</script>
</body>
</html>
Output:
Nested Arrays of Strings: Values inside an array can also be an array, known as Nested Arrays. Array can also have JSON object.
Example #4
Code:
<!DOCTYPE html>
<html>
<body>
<p>Looping through nested arrays</p>
<p id="demo"></p>
<script>
var sample, i, j, x = "";
sample = {
"empname":"Saideep",
"empage":40,
"empcars": [
{"carname":"BMW", "carmodels":["Maruti", "i10", "i20"]},
{"carname":"Hait", "carmodels":["Santro", "BMW", "Renault"]},
{"carname":"Nissan", "carmodels":["Alto", "Audi"] }
]
}
for (i in sample.empcars) {
x += "<h2>" + sample.empcars[i].carname + "</h2>";
for (j in sample.empcars[i].carmodels) {
x += sample.empcars[i].carmodels[j] + "<br>";
}
}
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Output:
Example #5
Code:
Deletion of value in an array of strings.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<p>Deletion of values in an array of strings</p>
<p id="paraId"></p>
<script>
var string = "";
var sample = {
"employeename": "Anusha",
"employername": "CPG",
"techskills": ["react", "JavaScript", "Java"]
};
delete sample.techskills[0];
for (i in sample.techskills) {
string += sample.techskills[i] + "<br/>";
}
document.getElementById("paraId").innerHTML = string;
</script>
</body>
</html>
Output:
Conclusion
We have seen how JSON array represents an ordered list of values be it string, numbers, Boolean, or an object. Here as per the topic, we have discussed how array of strings are accessed using for loop, how array value can be deleted, multi dimensional array, a nested array with examples listed above for each. Even though JSON array is similar to JavaScript array, array values are accessed using the index of each element in Array.
Recommended Articles
This is a guide to JSON Array of Strings. Here we also discuss the introduction and how json array of string works? along with different examples and its code implementation. You may also have a look at the following articles to learn more –