Skip to content

JavaScript console log object as JSON | Example code

  • by

The simplest way is using the Console log() method to log objects as JSON in JavaScript. The console.log() method called with an object or objects as arguments will display the object or objects.

console.log(object)

JavaScript console log object as JSON

Simple example code direct pass object into a console.log() method.

<!DOCTYPE html>
<html>
<body>

  <script>
   var obj = {
    "prop_1": {
      "prop_11": "val_11",
      "prop_12": "val_12"
    },
    "prop_2": "val_2",
    "prop_3": "val_3"
  };     
  console.log(obj)
</script>

</body>
</html>

Output:

JavaScript console log object as JSON

Use console.log(JSON.stringify(object))

The JSON.stringify() method converts a JavaScript object or value to a JSON string.

<script>
   var obj = {
    "prop_1": {
      "prop_11": "val_11",
      "prop_12": "val_12"
    },
    "prop_2": "val_2",
    "prop_3": "val_3"
  };     
  console.log(JSON.stringify(obj))
</script>

Output:

{“prop_1”:{“prop_11″:”val_11″,”prop_12″:”val_12″},”prop_2″:”val_2″,”prop_3″:”val_3”}

Read more: Console log object in JavaScript | Multiple ways

Do comment if you have any doubts or suggestions on this JS log object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading