• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

REST API Tutorial

  • REST
  • JSON
  • Dark Mode
Home / JSON / JSON Object

JSON Object

The JSON object data type is a list of name-value pairs surrounded in curly braces.

  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.

A JSON object example is:

{
	"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;

Was this article helpful?

Share this:

  • Twitter
  • Facebook
Previous Tutorial:
JSON Array
Next Tutorial:
JSON with Ajax

Reader Interactions

Comments

  1. Dan says

    March 15, 2020 at 6:19 pm

    Regarding “Delete object values”
    I’m going to guess that you can delete object properties using the ([]) bracket notation, as well as using the dot (.) notation?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Search Tutorials

JSON Tutorial

  • What is JSON
  • JSON Syntax
  • JSON Data Types
  • JSON Schema
  • JSON Object
  • JSON Array
  • JSON parse()
  • JSON stringify()
  • JSON vs XML
  • JSON with Ajax
  • JSONPath

Footer

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

Copyright © 2020 · restfulapi.net · All Rights Reserved. | Sitemap