loading...
Cover image for Node.js read and write post status to a JSON file

Node.js read and write post status to a JSON file

dailydevtips1 profile image Chris Bongers Originally published at daily-dev-tips.com ・2 min read

A while ago, I started building my RSS reader to auto-publish to certain platforms, but we never finished it πŸ‘€.

Today we will be looking into looping over the articles we get via the RSS reader and keeping track of which ones are posted to the socials.

Pre-condition: You need to know how to set up a basic node app πŸ‘ˆ

What you'll learn from this article

  • Read data from a JSON file in Node.js
  • Write data to a JSON file in Node.js
  • Reading RSS data
  • Keeping track of run changes

Setting up our JSON file

Our JSON file is going to be quite easy in structure and will look as follows:

{
    "https://daily-dev-tips.com/posts/rss-reader-in-node-js": {
        "published": true
    },
    "https://daily-dev-tips.com/posts/top-10-chrome-extensions-for-developers-πŸ‘€": {
        "published": true
    }
}

We basically only need to know if an object is already on this list.

Looping through our RSS feed

First, we need to add the rss-parser package.

npm i rss-parser

Then we can loop through our articles using the sitemap we have.

let Parser = require('rss-parser');
let parser = new Parser();

(async () => {
    let feed = await parser.parseURL('https://daily-dev-tips.com/sitemap.xml');

    feed.items.forEach(item => {
        console.log(item.id);
    });
})();

Now we need to make sure we read our JSON file and see if we have already published this article.

First, let's define the file-system.

const fs = require('fs');

Then we can read out actual JSON file

let rawdata = fs.readFileSync('site.json');
let siteData = JSON.parse(rawdata);

This will at first we an empty object {}.

In our loop we need to check if we already published this item.
If yes => Don't do anything
If no => Do magic and then add to JSON file.β€Œ

feed.items.forEach(item => {
    let url = item.id;
    if (!siteData.url) {
        // Do magic posting stuff
        siteData[url] = {
            'published': true
        };
    }
});

Once the loop is done, we can save our JSON to the actual file.

fs.writeFileSync('site.json', JSON.stringify(siteData));

Our JSON file will then look something like this.

{
    "https://daily-dev-tips.com/posts/vanilla-javascript-canvas-images-to-black-and-white/": {
        "published": true
    },
    "https://daily-dev-tips.com/posts/vanilla-javascript-images-in-canvas/": {
        "published": true
    },
    "https://daily-dev-tips.com/posts/vanilla-javascript-colouring-our-canvas-elements-🌈/": {
        "published": true
    }
}

Awesome, we now parsed our RSS feed, read our JSON file, and wrote data to it if not already in there!

You can find this project on GitHub.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Posted on by:

dailydevtips1 profile

Chris Bongers

@dailydevtips1

I write daily web development tips that help you become a more efficient developer. πŸ‘¨β€πŸ’»βš‘οΈ

Discussion

pic
Editor guide