Skip to content
Related Articles

Related Articles

Backbone.js toJSON Model

View Discussion
Improve Article
Save Article
  • Last Updated : 26 Jun, 2022

The Backbone.js clone is used to return the attributes of the given object in JSON format. We have to use JSON.stringify() to return the attributes.

Syntax:

Backbone.Model.toJSON(options)

Parameters:

  • options: Used to take the attribute name.

If it is not specified, then it will return the whole model.

Example 1: In this example, we will display all the attributes in a book model.

HTML




<!DOCTYPE html>
<html>
  
<head>
      
    <script src=
            type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
      
</head>
  
<body>
    <script type="text/javascript">  
        var Books = Backbone.Model.extend();  
        var book = new Books(
              {
              book_name:"css",
              price:900,
              type:"web"
            });  
        document.write("Values in book model:  ", 
                       JSON.stringify(book));        
              
    </script
</body>
</html>


Output:

Values in book model:
{
    "book_name":"css",
    "price":900,
    "type":"web"
}

Example 2: The following code demonstrates the toJSON model with an empty object.

HTML




<!DOCTYPE html>
<html>
  
<head>
      
    <script src=
            type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
      
</head>
  
<body>
    <script type="text/javascript">  
        var Books = Backbone.Model.extend();  
        var book = new Books();  
        document.write("Values in book model:  ", 
                       JSON.stringify(book));      
              
    </script
</body>
</html>


Output:

Values in book model: {}

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!