Skip to content

Console log JSON stringify | JavaScript example

  • by

Console log JSON stringify means converting a JavaScript object or value to a JSON string. The console log will show the string output.

JavaScript Console log JSON stringify

Simple example code.

<!DOCTYPE html>
<html>
<body>

  <script>
   const sendquery = {
    "async": true,
    "crossDomain": true,
    "url": "xyz.com",
    "method": "POST",
    "headers": {
      "Authorization": "Bearer " + "tkid",
      "Accept": "application/json",
      "Content-Type": "application/json",
    },
    "processData": false,
    "data": "query"
  };

  console.log(JSON.stringify(sendquery));
</script>

</body>
</html>

Output:

Console log JSON stringify

console.log inconsistent with JSON.stringify

Use console.dir(obj) instead? Or use console.log(obj) and then click on the output / expand it.

<script>
   var obj = {};
   obj.players = {};
   obj.players[0] = {color: "green"};
   obj.players[1] = {color: "blue"};
   obj.world = "xyz";
   
   console.log(JSON.stringify(obj));
 </script>

I always (Firefox, Chrome, Opera) get this as output:

{"players":{"0":{"color":"green"},"1":{"color":"blue"}},"world":"xyz"}

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

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

OS: Windows 10

Code: HTML 5 Version

Tags:

Leave a Reply

Discover more from Tutorial

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

Continue reading