{"id":259,"date":"2018-05-01T07:39:16","date_gmt":"2018-05-01T07:39:16","guid":{"rendered":"https:\/\/restfulapi.net\/?p=259"},"modified":"2021-09-29T13:01:12","modified_gmt":"2021-09-29T07:31:12","slug":"json-array","status":"publish","type":"post","link":"https:\/\/restfulapi.net\/json-array\/","title":{"rendered":"JSON Array"},"content":{"rendered":"\n
Similar to other programming languages, a JSON<\/a> Array is a list of items surrounded in square brackets ([])<\/strong>. Each item in the array is separated by a comma<\/strong>.<\/p>\n\n\n\n For example, given below is a JSON document that contains a JSON array of access rights.<\/p>\n\n\n\n You can access the array values by using the index number:<\/p>\n\n\n\n Program output.<\/p>\n\n\n\n Use the delete keyword to delete items from an array:<\/p>\n\n\n\n Use the index number to modify an array:<\/p>\n\n\n\n We can access array values by using a for-in loop:<\/p>\n\n\n\n Program output.<\/p>\n\n\n\n We can store an array inside another JSON array. It is known as an array of arrays or a multi-dimensional JSON array.<\/p>\n\n\n\n <\/p>\n\n\n\n A simple for loop to iterate over a multi-dimensional array in JSON.<\/p>\n\n\n\n Program Output:<\/p>\n\n\n\n 1. Array Datatype in JSON 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 … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":249,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[17],"_links":{"self":[{"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/posts\/259"}],"collection":[{"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/comments?post=259"}],"version-history":[{"count":0,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/posts\/259\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/media\/249"}],"wp:attachment":[{"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/media?parent=259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/categories?post=259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/tags?post=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}[...]<\/code> are used to declare JSON array.<\/li>
string<\/code>,
number<\/code>,
boolean<\/code>,
object <\/code>or other array inside JSON array.<\/li>
{\n\t\"name\" : \"Admin\",\n\t\"age\" : 36,\n\t\"rights\" : [ \"admin\", \"editor\", \"contributor\" ]\n}<\/code><\/pre>\n\n\n\n
2. Array Operations<\/h2>\n\n\n\n
2.1. Get Value from Array<\/h4>\n\n\n\n
x = myObj.rights[0];<\/code><\/pre>\n\n\n\n
admin<\/code><\/pre>\n\n\n\n
2.2. Delete Array Value<\/h4>\n\n\n\n
delete myObj.rights[1];<\/code><\/pre>\n\n\n\n
2.3. Update Array Value<\/h4>\n\n\n\n
myObj.rights[1] = \"blogger\";<\/code><\/pre>\n\n\n\n
2.4. Looping through Array Values<\/h4>\n\n\n\n
for (i in myObj.rights) \n{\n\tx = myObj.rights[i];\n\tconsole.log(x);\n}<\/code><\/pre>\n\n\n\n
admin\neditor\ncontributor<\/code><\/pre>\n\n\n\n
3. Multi-dimensional Arrays<\/h2>\n\n\n\n
3.1. Creating a multi-dimensional array <\/h4>\n\n\n\n
var siteInfo = {\n\t\"name\" : \"blogger\",\n\t\"users\" : [\n\t\t[ \"admins\", \"1\", \"2\" , \"3\"],\n\t\t[ \"editors\", \"4\", \"5\" , \"6\"],\n\t]\n}<\/code><\/pre>\n\n\n\n
3.2. Iterating over multi-dimensional array<\/h4>\n\n\n\n
for (i in siteInfo .users) \n{\n\tfor (j in siteInfo.users[i]) \n\t{\n\t\tx = siteInfo.users[i][j];\n\t\tconsole.log(x);\n\t}\n}<\/code><\/pre>\n\n\n\n
admins\n1\n2\n3\neditors\n4\n5\n6<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"