Languages
[Edit]
EN

Print JSON to console in JavaScript

5 points
Created by:
Root-ssh
69140

Hi, today I would like to show you how to print json to console using JavaScript.

// ONLINE-RUNNER:browser;

var object = {"id": 2, "name": "Tom", "age": 25};

console.log(JSON.stringify(object)); // {"id":2,"name":"Tom","age":25}

Note: using JSON.stringify(userObj) we convert object to JSON and print to console.

Other type examples

It is possible to convert different types to JSON. Types like: boolean, number, string, object, etc.

Practical example:

// ONLINE-RUNNER:browser;

var boolean = true;
var number = 2;
var string ='Some text inside...';
var object = {'id':2,'name':'Tom','age':25};

function printJSON(entry) {
	console.log(JSON.stringify(entry));
}

printJSON(boolean); // true
printJSON(number);  // 2
printJSON(string);  // Some text inside...
printJSON(object);  // {"id":2,"name":"Tom","age":25}
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join