简体   繁体   中英

How do I transfer data from console log to a json file?

There is a script that outputs data to console. log, how can I put data from console.log in a separate json file?

var store = require('app-store-scraper');

 store.search({
    term: 'ninja',
    num: 2,
    page: 3,
    country : 'us',
    lang: 'lang'
    })
  .then(console.log)
  .catch(console.log);

You can use the fs module as:

var fs = require('fs');
var jsonData = {}    // Your Json data 
fs.writeFile ("output.json", JSON.stringify(jsonData), function(err) {
    if (err) throw err;
    console.log('complete');
    }
);

The easiest way around is to use >> operator

node your_file.js >> file.json

Here's the solution:

const fs = require('fs');
const store = require('app-store-scraper');

store.search({
    term: 'ninja',
    num: 2,
    page: 3,
    country : 'us',
    lang: 'lang'
})
.then(response => {
    fs.writeFileSync('file.json', JSON.stringify(response));
})
.catch(err => {
    fs.writeFileSync('file.json', JSON.stringify(err));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM