qnd:json

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
qnd:json [2021/12/15 20:50] mguptonqnd:json [2021/12/16 14:09] (current) mgupton
Line 1: Line 1:
 ====== JSON ====== ====== JSON ======
  
-=== Convert JS Object to JSON text ===+=== Serialize JS Object to JSON===
  
-<code>+<code javascript>
 var obj = {name: "Martin", age: 30, country: "United States"}; var obj = {name: "Martin", age: 30, country: "United States"};
          
Line 11: Line 11:
 </code> </code>
  
-=== Write JSON to file ===+=== Read in JS object and write out JSON to file ===
  
-<code>+<code javascript>
 const fs = require('fs') const fs = require('fs')
  
 +// Read JS object from file
 const data = require('./data.js'); const data = require('./data.js');
  
-var json = JSON.stringify(data);+// Convert object defined in data.js 
 +var json = JSON.stringify(obj_from_data);
  
-fs.writeFile('/Users/joe/test.txt', json, err => {+fs.writeFile('/tmp/data.json', json, err => {
   if (err) {   if (err) {
     console.error(err)     console.error(err)
  • qnd/json.1639601440.txt.gz
  • Last modified: 2021/12/15 20:50
  • by mgupton