REST API Tutorial

  • REST
  • JSON
  • Dark Mode

JSON Objects

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

For example –

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

Access object values

You can access object values in two ways :

1) Using dot (.) notation

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

console.log( author.name );

//Output

Admin

2) Using bracket ([]) notation

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

console.log( author [ "name" ] );
console.log( author [ "age" ] );

//Output

Admin
36

Looping object values

You can loop through object values using for loop, just like looping through an array.

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

for (x in author) 
{
    console.log(x + " - " + (author[x]));
}

//Output

name - Admin
age - 36
rights - admin,editor,contributor

Modify object values

To modify object values, use any of given two ways:

1) Using dot (.) notation

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

author.name = "Lokesh";

console.log( author.name );

//Output

Lokesh

2) Using bracket ([]) notation

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

author["name"] = "Lokesh";
author["age"] = 35;

console.log( author [ "name" ] );
console.log( author [ "age" ] );

//Output

Lokesh
35

Delete object values

Use the delete keyword to delete properties from a JSON object:

delete author.name;

TwitterFacebookLinkedinReddit

Leave a Reply

This comment form is under antispam protection
This comment form is under antispam protection
  Subscribe  
Notify of

JSON Tutorial

  • Introduction to JSON
  • JSON Syntax
  • JSON Data Types
  • JSON Schema
  • JSON Objects
  • JSON Array
  • JSON.parse()
  • JSON.stringify()
  • JSON vs XML
  • JSON with Ajax
  • JSONPath

References

  • The dissertation by Roy Thomas Fielding
  • Uniform Resource Identifier (URI, URL, URN) [RFC 3986]
  • Internet MediaTypes
  • Web Application Description Language (WADL)

Meta Links

  • About
  • Contact Us
  • Privacy Policy

Blogs

  • How To Do In Java
Creative Commons License
This work by RESTfulAPI.net is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
wpDiscuz