
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to format JSON string in JavaScript?
To format HSON string in JavaScript, use JSON.stringify() with some parameters. Following is the code −
Example
var details = { studentId: 101, studentFirstName: 'David', studentLastName: 'Miller', studentAge:21, subjectDetails: { subjectId: 'JavaScript_101', subjectName: 'Introduction to JavaScript', } }; console.log("Not Pretty JSON Format=") console.log(JSON.stringify(details)); console.log("Pretty JSON Format=") console.log(JSON.stringify(details, null, 3));
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo127.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo127.js Not Pretty JSON Format= {"studentId":101,"studentFirstName":"David","studentLastName":"Miller","studentAge":21,"subj ectDetails":{"subjectId":"JavaScript_101","subjectName":"Introduction to JavaScript"}} Pretty JSON Format={ "studentId": 101, "studentFirstName": "David", "studentLastName": "Miller", "studentAge": 21, "subjectDetails": { "subjectId": "JavaScript_101", "subjectName": "Introduction to JavaScript" } }
- Related Articles
- Transform tree from DB format to JSON format in JavaScript
- Convert JSON to another JSON format with recursion JavaScript
- How to convert MySQL DATETIME value to JSON format in JavaScript?
- How to convert JSON string to array of JSON objects using JavaScript?
- How to convert Python date in JSON format?
- How to print Python dictionary into JSON format?
- How to convert a JSON string into a JavaScript object?
- How to create JSON format with group-concat in MySQL?
- Explain JSON format in PowerShell.
- How to convert JSON object to Hashtable format using PowerShell?
- How to convert a date object to string with format hh:mm:ss in JavaScript?
- How to parse json string in android?
- How to convert JSON text to JavaScript JSON object?
- How can I represent python tuple in JSON format?
- How to format date string in PowerShell?

Advertisements