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:52] 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 javascript> <code javascript>
Line 11: Line 11:
 </code> </code>
  
-=== Write JSON to file ===+=== Read in JS object and write out JSON to file ===
  
 <code javascript> <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');
  
-// Include JS file that defines some JS objects +// Convert object defined in data.js 
-var json = JSON.stringify(data);+var json = JSON.stringify(obj_from_data);
  
-// Convert object reference by var_from_data +fs.writeFile('/tmp/data.json', json, err => {
-fs.writeFile('/Users/joe/test.txt', var_from_data, err => {+
   if (err) {   if (err) {
     console.error(err)     console.error(err)
  • qnd/json.1639601576.txt.gz
  • Last modified: 2021/12/15 20:52
  • by mgupton