1

I am using ExpressJS to write my application and jsonfile (https://www.npmjs.com/package/jsonfile) to handle json files. I have this following json file:

{
  "news": [
    {
      "id": "1",
      "title": "News 1 heading",
      "description": "Lorem ipsum dolor sit amet upidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
      "dateposted": "00188292929"
    },
    {
      "id": "2",
      "title": "News 2 heading",
      "description": "Lorem ipsum dolor sit amet",
      "dateposted": "00188292929"
    }
  ]
}

Now, I want to add another set of news under the "news" node, so that my final json looks like this:

{
      "news": [
        {
          "id": "1",
          "title": "News 1 heading",
          "description": "Lorem ipsum dolor sit amet upidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
          "dateposted": "00188292929"
        },
        {
          "id": "2",
          "title": "News 2 heading",
          "description": "Lorem ipsum dolor sit amet",
          "dateposted": "00188292929"
        },
        {
          "id": "3",
          "title": "News 3 heading",
          "description": "Lorem ipsum dolor sit amet",
          "dateposted": "00188292929"
        }
      ]
    }

There is an append flag with jsonfile but it appends at the end of the file rather than under a given node. How can I append the data under and existing node? Do, I need to stringify the json, add data and JSONfy it? or there is a more direct way?

Thanks.

2 Answers 2

1

You can use Json PUSH to append a json object to a current node. The code would look like this:

var json={
      "news": [
        {
          "id": "1",
          "title": "News 1 heading",
          "description": "Lorem ipsum dolor sit amet upidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
          "dateposted": "00188292929"
        },
        {
          "id": "2",
          "title": "News 2 heading",
          "description": "Lorem ipsum dolor sit amet",
          "dateposted": "00188292929"
        },
        {
          "id": "3",
          "title": "News 3 heading",
          "description": "Lorem ipsum dolor sit amet",
          "dateposted": "00188292929"
        }
      ]
    };


    json.news.push({
          "id": "3",
          "title": "News 3 heading",
          "description": "Lorem ipsum dolor sit amet",
          "dateposted": "00188292929"
        });
   console.log(json);
   

Sign up to request clarification or add additional context in comments.

Comments

0

Jsonfile's append option is referring to opening a file in append mode, in this mode you can only add to the end of the file.

You will need to re-write the entire file using the normal writeFile options. Effectively overwriting the original file.

You can see in the jsonfile code on line 91 (it is a short single file node module) that it simply passes the append flag through to fs.writeFile. I'm not entirely sure when you would use this in all honesty, but I'm assuming it's if you want to maybe output a bunch of documents and then append on some json at the bottom of each.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.