Hi all I am loading XML file with file name 'Attachments.xml' using the following code:
var attachmentsXml = XmlBuddy.load(file, 'Attachments.xml');
Now if I want to load the xml-file with file name as 'attachments.xml' I am unable to load so can anyone suggest me how to do.
Most likely you are using node.js so you can use the xml2json
fs = require('fs');
var parser = require('xml2json');
fs.readFile( './Attachments.xml', function(err, data) {
var json = parser.toJson(data);
console.log("to json ->", json);
});
Related
I have a dummy csv file uploaded in my nodejs application. All I want is to convert the CSV to JSON file.
But it gives syntax error while just requiring the csv file.
Below is the code in nodejs to parse file content
const csv = require('csv-parser')
const results = [];
let csvToJson = require('convert-csv-to-json');
let fileInputName = require('./fileUpload/testingCSV.csv');
let fileOutputName = 'myproduct.json';
const fs = require('fs');
fs.createReadStream(fileInputName)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
console.log(results);
});
csvToJson.generateJsonFileFromCsv(fileInputName,fileOutputName);
Below is the screenshot of console error
Also, the actual csv file will contain a large number of data of products/accounts/userprofiles etc.
this is a dummy csv that I am trying to parse.
Please let me know what needs to be done .
I think your csv file is not correcly formated, what is the name of your nodejs file, and can you give us the error line 14 please ?
EDIT :
you are probably using an old version of Node.js. If this is the case, either const csv = require('csv-parser'); to const csv = require('csv-parser/lib/es5');`
Or
the csv file which is incorrect. Give the path directly on createreadstream
This question already has answers here:
Download JSON object as a file from browser
(14 answers)
Closed 3 years ago.
I want to generate a .json file on my local project folder. I want to save the fetch API call response (which is an array of objects) into a .json file Here is my code :
ts :
getRecords(){
this.service.getRecords()
.subscibe((res)=>{
// res.data -want to convert this data into .json file
});
}
I'm not sure if you want to be able to do this locally (i.e., so the user can download a JSON file), or if you want to do this using Node.js, so here's both approaches.
I mention Nodejs since you tagged this with Angular.
If you are using NodeJS, you can use the fs module:
let json = {"foo": "bar"};
let str = JSON.stringify(json);
let fs = require("fs");
fs.writeFile("jsonFile.json", str, function(error) {
if (error) {
console.log("Error");
} else {
console.log("Success");
}
);
But if you want to create a file from the browser and download on the client machine:
let json = {"foo": "bar"};
let str = JSON.stringify(json);
let str = "data:text/json;charset=utf-8," + encodeURIComponent(str);
let dl = document.createElement("a");
dl.setAttribute("href", str);
dl.setAttribute("download", "jsonFile.json");
dl.click();
Also, your code did not get added to your question.
I just want to store my json data in a file in a particular directory using JS. I can not see the created file using the following code.
var jsonse = JSON.stringify(submitted);
var blob = new Blob([jsonse], {type: "application/json"});
var file = new File([blob], "" + workerID + ".json")
JS Documentation Link would also suffice.
Assuming you're not using a web browser which cannot write to your file system for, hopefully obvious (another question), security reasons.
You can redirect output from your script to a file.
node yourfile.js > output_file.json
Or you can use the fs module.
Writing files in Node.js
// note jsonse is the json blob
var fs = require('fs');
fs.writeFile("/tmp/test", jsonse, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
I am making a program in nwjs, which will be used to reading part of .json file and showing it + giving possibility to edit it.
And I don't know how can I read, show and edit part of .json file.
Any ideas?
var filesystem = require('fs');
Then refer to the Node documentation here: NodeJS FS
You'll want to load the json file into an object, and then save changes to the file periodically or manually, rather than constantly loading and saving the file.
You can use the jsonfile package to read and write the object.
Read JSON file to object:
var jsonfile = require('jsonfile');
var file = '/path/to/file.json';
jsonfile.readFile(file, function(err, someData) {
if (err) {
// don't use someData
// it's not populated
return;
}
// do something with someData
// there was no error
console.log(someData);
});
Write object to JSON file:
var jsonfile = require('jsonfile');
var file = '/path/to/file.json';
var data = { name: 'John Doe', age: 50, eyes: 'brown', hair: 'black' };
jsonfile.writeFile(file, data, function (err) {
// an error occurred
console.error(err);
});
Best practice is to validate complex data using JSON-schema validation. There are countless JSON editor tools available on GitHub such as JSON Editor Online and Treema. These have been implemented in JavaScript and jQuery and shouldn't prove difficult to follow.
I am currently requiring a JSON file which I am reading data from.
var allUORHours = require('./UORHoursAch.json');
How do I then write to the file? The below doesn't make any changes to the file
allUORHours.test = {};
You may use the File System API's writeFile():
https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback
No, of course it doesn't. It just changes the variable's value. To write a JSON, you would need to convert to JSON, then write to a file:
var fs = require('fs');
fs.writeFile('./UORHoursAch.json', JSON.stringify(allUORHours), function (err) {
if (err) {
console.log(err);
} else {
console.log("Saved");
}
});