JavaScript में JSON स्ट्रिंग को फॉर्मेट करने के लिए, पैरामीटर्स के साथ JSON.stringify() का उपयोग करें। कोड निम्न है:
उदाहरण
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));
उपरोक्त प्रोग्राम चलाने के लिए, आपको निम्न कमांड का उपयोग करना होगा:
node fileName.js.
यहां, मेरी फाइल का नाम demo127.js है।
आउटपुट
यह निम्न आउटपुट उत्पन्न करेगा:
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" } }