Is a list a JSON object?

Similar to other programming languages, a JSON Array is a list of items surrounded in square brackets ([]). Each item in the array is separated by a comma.

  • The array index begins with 0.
  • The square brackets [...] are used to declare JSON array.
  • JSON array are ordered list of values.
  • JSON arrays can be of multiple data types.
  • JSON array can store string, number, boolean, object or other array inside JSON array.
  • In JSON array, values must be separated by comma.
  • Arrays in JSON are almost the same as arrays in JavaScript.

For example, given below is a JSON document that contains a JSON array of access rights.

{ "name" : "Admin", "age" : 36, "rights" : [ "admin", "editor", "contributor" ] }

2. Array Operations

2.1. Get Value from Array

You can access the array values by using the index number:

x = myObj.rights[0];

Program output.

admin

2.2. Delete Array Value

Use the delete keyword to delete items from an array:

delete myObj.rights[1];

2.3. Update Array Value

Use the index number to modify an array:

myObj.rights[1] = "blogger";

2.4. Looping through Array Values

We can access array values by using a for-in loop:

for (i in myObj.rights) { x = myObj.rights[i]; console.log(x); }

Program output.

admin editor contributor

3. Multi-dimensional Arrays

3.1. Creating a multi-dimensional array

We can store an array inside another JSON array. It is known as an array of arrays or a multi-dimensional JSON array.

var siteInfo = { "name" : "blogger", "users" : [ [ "admins", "1", "2" , "3"], [ "editors", "4", "5" , "6"], ] }

3.2. Iterating over multi-dimensional array

A simple for loop to iterate over a multi-dimensional array in JSON.

for (i in siteInfo .users) { for (j in siteInfo.users[i]) { x = siteInfo.users[i][j]; console.log(x); } }

Program Output:

admins 1 2 3 editors 4 5 6

Let us know if you liked the post. That’s the only way we can improve.

Is a list a JSON object?

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:

Is a list a JSON object?

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:

Is a list a JSON object?

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":"", "age":73}, {"empname ":"Shyam", "empeemail":"", "age":88}, {"empname ":"Saideep", "empemail":"", "age":33}, {"empname ":"Anusha", "empemail":"", "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:

Is a list a JSON object?

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:

Is a list a JSON object?

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:

Is a list a JSON object?

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.

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 –

  1. JSON Stringify Pretty
  2. JSON Pretty
  3. JSON.stringify JavaScript
  4. PostgreSQL JSON