{"id":260,"date":"2018-05-01T08:14:28","date_gmt":"2018-05-01T08:14:28","guid":{"rendered":"https:\/\/restfulapi.net\/?p=260"},"modified":"2021-09-27T22:48:22","modified_gmt":"2021-09-27T17:18:22","slug":"json-objects","status":"publish","type":"post","link":"https:\/\/restfulapi.net\/json-objects\/","title":{"rendered":"JSON Object"},"content":{"rendered":"\n

The JSON object data type is a list of name-value pairs surrounded in curly braces.<\/p>\n\n\n\n

  1. JSON objects are very much like javascript objects.<\/li>
  2. JSON objects are written in key\/value pairs.<\/li>
  3. JSON objects are surrounded by curly braces { }<\/code>.<\/li>
  4. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null).<\/li>
  5. Keys and values are separated by a colon.<\/li>
  6. Each key\/value pair is separated by a comma.<\/li><\/ol>\n\n\n\n

    A JSON object example is:<\/p>\n\n\n\n

    {\r\n\t\"name\" : \"Admin\",\r\n\t\"age\" : 36,\r\n\t\"rights\" : [ \"admin\", \"editor\", \"contributor\" ]\r\n}<\/code><\/pre>\n\n\n\n

    1. Access object values<\/h2>\n\n\n\n

    You can access object values in two ways :<\/p>\n\n\n\n

    1.1. Using dot (.) notation<\/h4>\n\n\n\n
    var author = {\r\n\t\"name\" : \"Admin\",\r\n\t\"age\" : 36,\r\n\t\"rights\" : [ \"admin\", \"editor\", \"contributor\" ]\r\n}\r\n\r\nconsole.log( author.name );\r\n\r\n\/\/Output\r\n\r\nAdmin<\/code><\/pre>\n\n\n\n

    1.2. Using bracket ([]) notation<\/h4>\n\n\n\n
    var author = {\r\n\t\"name\" : \"Admin\",\r\n\t\"age\" : 36,\r\n\t\"rights\" : [ \"admin\", \"editor\", \"contributor\" ]\r\n}\r\n\r\nconsole.log( author [ \"name\" ] );\r\nconsole.log( author [ \"age\" ] );\r\n\r\n\/\/Output\r\n\r\nAdmin\r\n36<\/code><\/pre>\n\n\n\n

    2. Looping object values<\/h2>\n\n\n\n

    You can loop through object values using for loop, just like looping through an array.<\/p>\n\n\n\n

    var author = {\r\n\t\"name\" : \"Admin\",\r\n\t\"age\" : 36,\r\n\t\"rights\" : [ \"admin\", \"editor\", \"contributor\" ]\r\n}\r\n\r\n\/\/Looping\r\n\r\nfor (x in author)\r\n{\r\n\tconsole.log(x + \" - \" + (author[x]));\r\n}\r\n\r\n\/\/Output\r\n\r\nname - Admin\r\nage - 36\r\nrights - admin,editor,contributor<\/code><\/pre>\n\n\n\n

    3. Modify object values<\/h2>\n\n\n\n

    To modify object values, use any of the given two ways:<\/p>\n\n\n\n

    3.1. Using dot (.) notation<\/h4>\n\n\n\n
    var author = {\r\n\t\"name\" : \"Admin\",\r\n\t\"age\" : 36,\r\n\t\"rights\" : [ \"admin\", \"editor\", \"contributor\" ]\r\n}\r\n\r\nauthor.name = \"Lokesh\";\r\n\r\nconsole.log( author.name );\r\n\r\n\/\/Output\r\n\r\nLokesh<\/code><\/pre>\n\n\n\n

    3.2. Using bracket ([]) notation<\/h4>\n\n\n\n
    var author = {\r\n\t\"name\" : \"Admin\",\r\n\t\"age\" : 36,\r\n\t\"rights\" : [ \"admin\", \"editor\", \"contributor\" ]\r\n}\r\n\r\nauthor[\"name\"] = \"Lokesh\";\r\nauthor[\"age\"] = 35;\r\n\r\nconsole.log( author [ \"name\" ] );\r\nconsole.log( author [ \"age\" ] );\r\n\r\n\/\/Output\r\n\r\nLokesh\r\n35<\/code><\/pre>\n\n\n\n

    4. Delete object values<\/h2>\n\n\n\n

    Use the delete<\/code> keyword to delete properties from a JSON object:<\/p>\n\n\n\n

    delete author.name;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

    The JSON object data type is a list of name-value pairs surrounded in curly braces. JSON objects are very much like javascript objects. JSON objects are written in key\/value pairs. JSON objects are surrounded by curly braces { }. Keys must be strings, and values must be a valid JSON data type (string, number, object, … 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\/260"}],"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=260"}],"version-history":[{"count":0,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/posts\/260\/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=260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/categories?post=260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/tags?post=260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}