JSON Object | JSON Object Literal

JSON object literal has the following characteristics:

  • JSON object literal is written inside the curly braces {}.
  • It contains key/value pairs i.e {key1:value,key2:value2} that are separated by a colon : and key must be a string and always placed inside double quotes and value must be valid JSON data type such as string, number, object, array, boolean, null, etc.
  • The Key is always on the left side of the colon and it will be always string and must be placed inside the double quote.

Example

    
  {
   "name":"Smith", "salary":30000, "passion":"travelling"
}
   

JSON String & JSON Object Literal

When JSON object literal is surrounded by a single quote then it will be formed JSON string. Whenever a single quote is removed from the JSON string then it can be formed JSON object literal.

Below is an example of a JSON string.

    
   '{"name":"Smith", "salary":30000, "passion":"travelling"}'
   

JSON object literal is exist inside JSON string:

    
    {
     "name":"Smith", "salary":30000, "passion":"travelling"
   } 
   

Please keep in mind that JSON is a string, not an object.

Creating Javascript Object Using JSON Literal

Javascript object can be created by JSON object literal.

Source Code

          
            <script type="text/javascript">
   users = {"name":"John", "salary":45000};
</script>          
        
Try it now

Creating Javascript Object From JSON String

Javascript object is created by parsing JSON string. Let us see it.

Source Code

          
            <script type="text/javascript">
var users = '{"name":"Smith", "salary":45000}';
 jsObj = JSON.parse(users);
 </script>          
        
Try it now

Accessing Object Values

Object value can be access though the (.) operator.

Source Code

          
            <script type="text/javascript">
var users = '{"name":"Smith", "salary":45000}';
var obj = JSON.parse(users);
var userName = obj.name;
 </script>          
        
Try it now

object values can also be access by using bracket ([]) notation.

Source Code

          
            <script type="text/javascript">
var users = '{"name":"Smith", "salary":45000}';
 var obj = JSON.parse(users);
 var ActName = obj["name"];
</script>          
        
Try it now

Web Tutorials

JSON Object Literal
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4