I have the following code and I need to save allProducts in a .txt file with json format. How can I achieve that?
var fruit = [{"name: x","id: y",..}, {...},...]
var driedFruitsNut = [{"name: x","id: y",..}, {...},...]
fruit.forEach((item) =>{
item.category = "Fruits & Vegetables";
item.subtCategory = "Fruit";
});
driedFruitsNut.forEach((item) => {
item.category = "Fruits & Vegetables";
item.subtCategory = "Dried Fruits & Nuts";
});
var allProducts = fruit.concat(driedFruitsNut);
I tried something like this; but I have no idea if this is even a correct code:
var fs = require('fs');
fs.writeFile("test.txt", allProducts, function(err) {
if (err) {
console.log(err);
}
});
but when I run node fetchFruitVeg.js in my terminal. I get a text file looking like this:
You can try something like this:
var fs = require('fs');
fs.writeFile("test.txt", JSON.stringify(allProducts), function(err) {
if (err) {
console.log(err);
}
});
since allProducts is a javascript object, first you need to convert it into normal string data to write it in a .txt file or .json file
Related
This is my code. I am trying to output my data as CSV and I am being successful but I can't add the title of the columns. Although I saved the CSV file but the column titles are missing. Can anyone help in this?
var d= new Date();
var t=d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
var data = dataAWS
var result = data.map(function(val) {
return t+'","'+val.payload.test_data;
}).join('"\n"');
var csvData = '"'+ result +'"';
var fs = require('fs');
fs.writeFile("tempN.csv", csvData, 'utf8', function (err) {
if (err) {
console.log("An error occured while saving CSV.");
return console.log(err);
}
console.log("CSV file has been saved.");
});
Without knowing more I would suggest something like:
data.unshift(column_headers);
var result = data.map(function(val) {
return t+'","'+val.payload.test_data;
}).join("\n")
etc, etc
How can I find and edit an attribute value in a JavaScript file using nodeJS.
for example: If i have a file called app.js containing following code.
let title = "hello world";
document.getElementById("arr").innerHTML = title;
how can I change the value of title let title = "hello world"; or any other attribute using nodeJS.
Instead of trying to parse/edit JS files why not use something like JSON which is easy to serialize?
Your gulp task can write whatever values are needed to a file:
const fs = require('fs')
fs.writeFileSync('./blah.json', JSON.stringify({ title: "whatever" }))
Then your code references the JSON file
const title = require('./blah.json').title
document.getElementById("arr").innerHTML = title;
I was able to edit a JavaScript file using regex. my code is below:
function updateFile(filename, replacements) {
return new Promise(function(resolve) {
fs.readFile(filename, 'utf-8', function(err, data) {
var regex, replaceStr;
if (err) {
throw (err);
} else {
regex = new RegExp("(\\" + 'let' + "\\s* ]*" + replacements[0].rule + "\\s*=\\s*)([^\\n;}]+)([\\s*;}])");
replaceStr = "$1" + replacements[0].replacer + "$3";
data = data.replace(regex, replaceStr);
}
fs.writeFile(filename, data, 'utf-8', function(err) {
if (err) {
throw (err);
} else {
resolve();
}
});
});
})
}
usage:
var file = src/app.js
var myNewValue = "Hello";
updateFile(file, [{
rule: 'newApp',
replacer: myNewValue
}], function (err) {
sails.log.info((err));
});
this will edit,
let newApp = lol; to let newApp = Hello;
I have xpath example :element(by.xpath("xxx")) which will get me an output that needs to write into json file ,can someone help me on how to proceed in protractor.
var fs = require('fs'); // node.js file system
var filename = 'object.json';
element(by.xpath('xxx')).getText().then(function(text) {
if (text) {
var textAsJSON = JSON.parse(text);
return fs.writeFileSync(fileName, textAsJSON);
// or return fs.appendFileSync(filename, text);
}
});
I am trying to use csvtojson to convert a CSV file to JSON. I an using the csvtojson library and fs to read the file in. I can get it convert but, I cannot figure out how to access the variables afterwards. I am new to javascript, and the techniques that I thought would work coming from other languages have not worked... I am calling the function from another js file, and want to be able to access variables from the converter.on('end_parsed'...) function. Code Below
'use strict'
const fs = require('fs')
const Converter = require('csvtojson').Converter
exports.myfuncc = function () {
var converter = new Converter({})
var csvEncoding = { encoding: 'utf16le' }
var csvString = fs.readFileSync('C:\\Users\\jgipe\\Desktop\\csvs\\201508.txt', csvEncoding).toString()
converter.fromString(csvString, function (err, result) {
if (err) { console.log(err) }
})
converter.on('end_parsed', function (jsonArray) {
// Just examples of the data contained in one object
console.log(jsonArray[9]['Date'])
console.log(jsonArray[9]['Package Name'])
console.log(jsonArray[9]['Current Device Installs'])
console.log(jsonArray[9]['Daily Device Installs'])
console.log(jsonArray[9]['Daily Device Uninstalls'])
console.log(jsonArray[9]['Daily Device Upgrades'])
console.log(jsonArray[9]['Current User Installs'])
console.log(jsonArray[9]['Total User Installs'])
console.log(jsonArray[9]['Daily User Installs'])
console.log(jsonArray[9]['Daily User Uninstalls'])
})
}
Put the callback function as parameter for exports.myfuncc, like this:
'use strict'
const fs = require('fs')
const Converter = require('csvtojson').Converter
exports.myfuncc = function (callback) {
var converter = new Converter({})
var csvEncoding = { encoding: 'utf16le' }
var csvString = fs.readFileSync('C:\\Users\\jgipe\\Desktop\\csvs\\201508.txt', csvEncoding).toString()
converter.fromString(csvString, function (err, result) {
if (err) { console.log(err) }
})
converter.on('end_parsed', callback)
}
Then in another file call it like this:
exports.myfuncc(function(jsonArray){
// You can access jsonArray...
});
I am trying to convert a csv file to json using csvtojson converter in nodejs.My code is as shown below.
I get an output of data as shown below. I am not sure why this is happening and how to prevent it.
var Converter = require("csvtojson").Converter;
var fileStream = fs.createReadStream("input.csv");
var converter = new Converter({constructResult:false});
converter.on("end_parsed", function (jsonObj) {
var jsonfile = require('jsonfile');
var file = 'output.json';
jsonfile.writeFile(file, jsonObj, function (err) { console.error(err); });});
fileStream.pipe(converter);
{"��P\u0000a\u0000c\u0000k\u0000a\u0000g\u0000e\u0000 \u0000N\u0000a\u0000m\u0000e\u0000":"\u0000c\u0000o\u0000m\u0000.\u0000t\u0000r\u0000i\u0000n\u0000e\u0000t\u0000.\u0000h\u0000r\u0000p\u0000m\u0000o\u0000b\u0000i\u0000l\u0000e\u0000","\u0000A\u0000p\u0000p\u0000 \u0000V\u0000e\u0000r\u0000s\u0000i\u0000o\u0000n\u0000 \u0000C\u0000o\u0000d\u0000e\u0000":"\u00006\u00002\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000e\u0000r\u0000 \u0000L\u0000a\u0000n\u0000g\u0000u\u0000a\u0000g\u0000e\u0000":"\u0000e\u0000n\u0000","\u0000D\u0000e\u0000v\u0000i\u0000c\u0000e\u0000":"\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000S\u0000u\u0000b\u0000m\u0000i\u0000t\u0000 \u0000D\u0000a\u0000t\u0000e\u0000 \u0000a\u0000n\u0000d\u0000 \u0000T\u0000i\u0000m\u0000e\u0000":"\u00002\u00000\u00001\u00005\u0000-\u00001\u00002\u0000-\u00002\u00002\u0000T\u00000\u00003\u0000:\u00003\u00002\u0000:\u00003\u00008\u0000Z\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000S\u0000u\u0000b\u0000m\u0000i\u0000t\u0000 \u0000M\u0000i\u0000l\u0000l\u0000i\u0000s\u0000 \u0000S\u0000i\u0000n\u0000c\u0000e\u0000 \u0000E\u0000p\u0000o\u0000c\u0000h\u0000":"\u00001\u00004\u00005\u00000\u00007\u00005\u00005\u00001\u00005\u00008\u00002\u00006\u00002\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000L\u0000a\u0000s\u0000t\u0000 \u0000U\u0000p\u0000d\u0000a\u0000t\u0000e\u0000 \u0000D\u0000a\u0000t\u0000e\u0000 \u0000a\u0000n\u0000d\u0000 \u0000T\u0000i\u0000m\u0000e\u0000":"\u00002\u00000\u00001\u00005\u0000-\u00001\u00002\u0000-\u00002\u00002\u0000T\u00000\u00003\u0000:\u00003\u00002\u0000:\u00003\u00008\u0000Z\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000L\u0000a\u0000s\u0000t\u0000 \u0000U\u0000p\u0000d\u0000a\u0000t\u0000e\u0000 \u0000M\u0000i\u0000l\u0000l\u0000i\u0000s\u0000 \u0000S\u0000i\u0000n\u0000c\u0000e\u0000 \u0000E\u0000p\u0000o\u0000c\u0000h\u0000":"\u00001\u00004\u00005\u00000\u00007\u00005\u00005\u00001\u00005\u00008\u00002\u00006\u00002\u0000","\u0000S\u0000t\u0000a\u0000r\u0000 \u0000R\u0000a\u0000t\u0000i\u0000n\u0000g\u0000":"\u00005\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000T\u0000i\u0000t\u0000l\u0000e\u0000":"\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000T\u0000e\u0000x\u0000t\u0000":"\u0000","\u0000D\u0000e\u0000v\u0000e\u0000l\u0000o\u0000p\u0000e\u0000r\u0000 \u0000R\u0000e\u0000p\u0000l\u0000y\u0000 \u0000D\u0000a\u0000t\u0000e\u0000 \u0000a\u0000n\u0000d\u0000 \u0000T\u0000i\u0000m\u0000e\u0000":"\u0000","\u0000D\u0000e\u0000v\u0000e\u0000l\u0000o\u0000p\u0000e\u0000r\u0000 \u0000R\u0000e\u0000p\u0000l\u0000y\u0000 \u0000M\u0000i\u0000l\u0000l\u0000i\u0000s\u0000 \u0000S\u0000i\u0000n\u0000c\u0000e\u0000 \u0000E\u0000p\u0000o\u0000c\u0000h\u0000":"\u0000","\u0000D\u0000e\u0000v\u0000e\u0000l\u0000o\u0000p\u0000e\u0000r\u0000 \u0000R\u0000e\u0000p\u0000l\u0000y\u0000 \u0000T\u0000e\u0000x\u0000t\u0000":"\u0000","\u0000R\u0000e\u0000v\u0000i\u0000e\u0000w\u0000 \u0000L\u0000i\u0000n\u0000k\u0000":"\u0000"},
{"��P\u0000a\u0000c\u0000k\u0000a\u0000g\u0000e\u0000 \u0000N\u0000a\u0000m\u0000e\u0000":"\u0000"}
I resolved this issue: It was an encoding issue: correct code was "utf16 "
var csvEncoding = { encoding: 'utf16le' };
var csvString = fs.readFileSync(csvfile, csvEncoding).toString();
converter.fromString(csvString, function(err,result){
//your code here console.log(err);
console.log(result);
});
I had a very similar issue to OP but was using csv-parse with a file coming out of S3.
Thanks to OP I got on the right path with an encoding issue, I was able to resolve my issue by using utf16le in my stream coupled with iconv-lite like so:
s3
.getObject(getObjectParams)
.createReadStream()
.on('end', () => cb(null))
.pipe(iconv.decodeStream('utf16le'))
.pipe(parse({ delimiter: '\t', columns: true }))
.pipe(transformer);
Hopefully this helps others in the same boat!
var Converter = require("csvtojson").Converter;
var fs = require('fs');
var fileStream = fs.createReadStream("input.csv");
var converter = new Converter({constructResult:true});
converter.on("end_parsed", function (jsonObj) {
var jsonfile = require('jsonfile');
var file = 'output.json';
console.log(jsonObj);
jsonfile.writeFile(file, jsonObj, function (err,result) {
console.error(err);
console.log(result) ;
});
});
fileStream.pipe(converter);