nodejs append inside json file in array format

2024/2/27 7:45:42

I want the nodejs to append all the data in the JSON format in following format

    [{"name": "admin","message": "dfd","datetime": "2014-06-03 13:01:39"}, {"name": "admin","message": "dfd","datetime": "2014-06-03 13:01:39"},{"name": "admin","message": "dfd","datetime": "2014-06-03 13:01:39"}]

And I want the nodejs to append more data. I m trying append but it's appending like this

{"name": "admin","message": "dfd","datetime": "2014-06-03 13:01:39"
}{"name": "admin","message": "dfd","datetime": "2014-06-03 13:01:39"

I m using the following code to append

    myData= { "name": "sam" , "message": "hi how are you", "datetime": "2014-5-1 4:4:4" };fs.appendFile(outputFilename, JSON.stringify(myData, null, 4), function(err) {if(err) {console.log(err);}});

JSON is not 'appendable' format. You have two options here:

  1. Read file, parse it, append data to array, serialize, replace file content.
  2. Switch to different file format. Actually CSV is good enough to store table-like data and is 'appendable'.

Related Q&A

IndexedDB and many-to-many Relationships

How are you all handling many-to-many relationships in IndexedDB?For example, say I have a Blog object to hold a blog post and a Tag object for a tag/label of the blog post. One Blog can have many Tag…

how to use status codes 200, 404, 300 ( how jquery done and fail work internally) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

Can I determine if a device is in portrait or landscape mode using jquery?

I want to conditionally alter what the user sees on the photo gallery web site Im going to create based on if the device being used to view the site is in portrait/vertical vs. landscape/horizontal mod…

this bound to subscribe function and not outer component scope in Angular2

I am having an issue in one of my components in Angular2 where as this is bound to wrong context within one of my components. I have other components where this issue is not occurring but I cannot see …

Typescript: Add a function to moment.js namespace

Im trying to add extra functionality to moment.js library. I want to add a function that itself requires a moment() call in its body and Im having a hard time figuring this out.Im using the latest vers…

Display posts on blogger based on Label?

I need to make my homepage to display posts based on a certain label. I dont want all posts to show up at the section of homepage posts. I searched for away on Google Search and I found away that you r…

Nodejs Convert text to JSON

For some reason Im having such a hard time converting this txt file to an actual javascript array.myJson.txt{"action": "key press", "timestamp": 1523783621, "user&quo…

Firebase Auth and Google Calendar

What I am trying to do is use Firebase to authenticate with Google. Then get some data from Google Calendar. I got the first part working. I can authenticate with Google and get user name, email ect., …

Get the file names from multiple file upload field

I am trying to write an ajax uploader using Jquery. In the html I can write a multiple upload field like:<input type=file multiple=multiple name=files[]>But, how can I get the values from this fi…

onmouseover doesnt work when using javascript to add img tag on IE

I need some javascript code that dynamically adds an img tag to a div, and the img tag needs onmouseover and onmouseout handlers.I have it working on Firefox. But it doesnt quite work on IE. On IE, t…