update a json in node - node.js

{
aps: []
}
I read it like this:
let apartments = require("path to json file);
apartments.aps.push(apa); // apa is a valid object
fs.writeFile("path", JSON.stringify(apartments));
aps will contain objs like this
{ "id":0, "address": "something"}
when I push in my json file I see
[object Object]

Because apas is a string. JSON is a text format for representing JavaScript Objects (hence the name); you'll need to parse it (using a suitable library) before you can use it as an object.

Here's a simple working example:
const fs = require('fs');
const data = require('./message.json');
// add new value
data.new = 'new value';
fs.writeFile('message.json', JSON.stringify(data), (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
Original content:
{"a":1,"b":2}
Modified content:
{"a":1,"b":2,"new":"new value"}

Related

Insert new JSON data to the json file using nodejs [duplicate]

I am new to Node.js and JavaScript. I have a results.json file that I want to keep a running log of results from a script that pulls images from the web. However, my current script only overwrites the existing result. How do I build upon or add to the results.json so each subsequent result is logged in the results.json file? I would like it to be valid json.
Here is general example:
var currentSearchResult = someWebSearchResult
var fs = require('fs');
var json = JSON.stringify(['search result: ' + currentSearchResult + ': ', null, "\t");
fs.writeFile("results.json", json);
And the results.json:
[
"search result: currentSearchResult"
]
If you want the file to be valid JSON, you have to open your file, parse the JSON, append your new result to the array, transform it back into a string and save it again.
var fs = require('fs')
var currentSearchResult = 'example'
fs.readFile('results.json', function (err, data) {
var json = JSON.parse(data)
json.push('search result: ' + currentSearchResult)
fs.writeFile("results.json", JSON.stringify(json))
})
In general, If you want to append to file you should use:
fs.appendFile("results.json", json , function (err) {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
Append file creates file if does not exist.
But ,if you want to append JSON data first you read the data and after that you could overwrite that data.
fs.readFile('results.json', function (err, data) {
var json = JSON.parse(data);
json.push('search result: ' + currentSearchResult);
fs.writeFile("results.json", JSON.stringify(json), function(err){
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
})
Promise based solution [Javascript (ES6) + Node.js (V10 or above)]
const fsPromises = require('fs').promises;
fsPromises.readFile('myFile.json', 'utf8')
.then(data => {
let json = JSON.parse(data);
json.myArr.push({name: "Krishnan", salary: 5678});
fsPromises.writeFile('myFile.json', JSON.stringify(json))
.then( () => { console.log('Append Success'); })
.catch(err => { console.log("Append Failed: " + err);});
})
.catch(err => { console.log("Read Error: " +err);});
If your project supports Javascript ES8 then you could use asyn/await instead of native promise.
I have created data. Js file to maintain data displaying in the table fomat. Including text box to read data from user and how to display data enteted through text box in table

Nodejs . Getting [object,Object] when fs.writeFileSync

I am reading a csv file, then writing another in the format I need. console.log shows the data i need but the file I create shows [object, Object] for each line.
I am not sure why the console.log shows the correct data but the file does not. I've read over the Node documentation but I cannot figure this out. Any information you can provide is appreciated.
this is what console.log shows
var fs = require("fs");
var parse = require('csv-parse');
//read file
var inputFile = 'EXAMPLE_UPSdailyShipment.csv';
fs.createReadStream('EXAMPLE_UPSdailyShipment.csv', "utf8", function read(err, data) {
if (err) {
throw err;
}
content = data;
});
var arr = [];
//data for new file
var parser = parse({
delimiter: ","
}, function(err, data) {
data.forEach(function(column) {
// create line object, data from parsed fields, PackageReference1 is order#
var line = {
"BuyerCompanyName": " ",
"OrderNumber": column[8],
"ShipperName": "UPS",
"TrackingNumber": column[9],
"DateShipped": column[0],
"ShipmentCost": column[2],
};
arr.push(line);
});
console.log(arr);
fs.writeFileSync("newtemp3.csv", arr, 'utf8');
console.log("file read and new file created")
});
fs.createReadStream(inputFile).pipe(parser);
I think you just need to stringify the data first:
fs.writeFileSync("newtemp3.csv", JSON.stringify(arr), 'utf8');
Hopefully this solves your problem.

How to delete the first object from a JSON file

I'm trying to make a queue in node js and want to delete the first JSON object in my JSON file.
I create and store users in a JSON file and then add them in the queue JSON file by reading the users.json. The problem is that when i get the user from user.json it comes as an object within an array, and i cant filter to compare the id. So how can i do this?
// helper method to read JSON FILE.
const readFile = (callback, returnJson = false, filePath = dataPath, encoding = 'utf8') => {
fs.readFile(filePath, encoding, (err, data) => {
if (err) {
throw err;
}
callback(returnJson ? JSON.parse(data) : data);
});
};
// helper method to write on JSON FILE.
const writeFile = (fileData, callback, filePath = dataPath, encoding = 'utf8') => {
fs.writeFile(filePath, fileData, encoding, (err) => {
if (err) {
throw err;
}
callback();
});
};
// thats how i try to delete first user from the queue, but it deletes the user with index 1.
app.delete('/popLine', (req, res) => {
readFile(data => {
//const userId = req.params["id"];
delete data[1]; // remove the first element from the Line.
writeFile(JSON.stringify(data, null, 2), () => {
res.status(200).send(`queue id: removed`);
});
},
true);
});
// thats how an user is stored in queue.json
"5": [
{
"name": "teresa may",
"email": "parliament",
"gender": "male",
"id": 3
}
],
I would prefer loading the JSON file as a JSON object (if its manageable) then delete the first entry by key and then persisting the file back on the disk (over-write) periodically or instantaneously which be necessary.
You can do a require to load a JSON file as a JSON object. But note, if you do a re-require of the changed file to reload it again during runtime, it will not get you the changes you made to the file in between but the older content from last require. In that case you need to clear the item from the require cache before getting the changes.
I am assuming the file is in order of KBs.. for larger files I wouldn’t prefer this approach, rather I would figure a way out to get that file data in a NoSQL document database.
Hope this helps :)

Corrupted file using request js

I am using request js to download a file.
function requ(){
const options = {
uri: `api/tasks/${id}/attachments/${attachmentId}`
}
return rp.get(options)
}
My question is:
why piping to "res" like requ().pipe(res) works and returning the result of the request above using "send" like
requ().then((result)=>{
//here result is the file's representing string
res.send(result)
})
don't?
const fs = require('fs');
requ().then((result) => {
//here result is the file's representing string
const path = __dirname + '/tempFiles' + Date.now(); // a temporary file to send it
fs.writeFile(path, result, function(err) {
if(err) throw err;
return res.sendFile(path);
})
});
Read More About fs, link 2
My file was being corrupted because request was converting the response body to utf8. Using:
const options = {
uri: `api/tasks/${id}/attachments/${attachmentId}`,
encoding:null
}
fixed the problem

How convert word document to pdf in Nodejs

I have method on Node js Return Word document , i want pass parameter called file type to convert this word document
to PDF.
the output from buf var it file like below
PK
! ߤ�l [Content_Types].xml<?xml version="1.0" encoding="UTF-8" standalone="yes"?>...
How can convert this word document to pdf ?
router.get("/hello", function(request, response, next,fileType) {
var zip = new JSZip(
"UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnR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AAAACEAW239kwkBAADxAQAAFAAAAAAAAAAAAAAAAABsGAAAd29yZC93ZWJTZXR0aW5ncy54bWxQSwECLQAUAAYACAAAACEAAR0f33EBAADHAgAAEAAAAAAAAAAAAAAAAACnGQAAZG9jUHJvcHMvYXBwLnhtbFBLAQItABQABgAIAAAAIQDWVAxaegEAAAMDAAARAAAAAAAAAAAAAAAAAE4cAABkb2NQcm9wcy9jb3JlLnhtbFBLAQItABQABgAIAAAAIQALRmoQGwsAAARwAAAPAAAAAAAAAAAAAAAAAP8eAAB3b3JkL3N0eWxlcy54bWxQSwUGAAAAAAsACwDBAgAARyoAAAAA",
{ base64: true }
);
var doc = new Docxtemplater();
doc.loadZip(zip);
//set the templateVariables
doc.setData({});
try {
// render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
doc.render();
var buf = doc.getZip().generate({ type: "nodebuffer" });
if(fileType=='docx'){
response.writeHead(200, {
"Content-Disposition": "attachment;filename=" + "template.docx",
"Content-Type":
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
});
}else if(fileType=='pdf'){
//here should convert
}
response.end(buf);
} catch (error) {
var e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties
};
console.log(JSON.stringify({ error: e }));
// The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
throw error;
}
});
You could use a package for this. Although the package I found, require the file to be read from disk it seems and not from buffer.
docx-pdf: npm i docx-pdf
const docxConverter = require('docx-pdf');
docxConverter('./input.docx','./output.pdf', (err, result) => {
if (err) console.log(err);
else console.log(result); // writes to file for us
});
Another option is to use an external API like https://github.com/Api2Pdf/api2pdf.node#readme (you could maybe even host it yourself, if you're handling sensitive data).
Its pretty simple if you use libreoffice-convert library.
Code :
const libre = require('libreoffice-convert');
const fs = require('fs');
function convertToPdf(filePath) {
const file = fs.readFileSync(filePath);
libre.convert(file, extend, undefined, (err, done) => {
if (err) {
console.log(`Error converting file: ${err}`);
}
fs.writeFileSync('outputFile.pdf', done);
});
}
You can find more documentation here.

Resources