Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Backbone.js toJSON Model

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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
Last Updated : 26 Jun, 2022
Like Article
Save Article
Similar Reads
Related Tutorials