0

I want to be able to only save specific data from the output of the API...

function getGiph() {
    giphy.search({
        q: 'pratt',
    }, function (err, res) {
        var gif = res.data;
        for (var i = 0; i < gif.length; i++) {
            var embed = gif[i].embed_url;
            fs.writeFile('./apidata.json', JSON.stringify(embed, undefined, 1), function (err) {
                if (err) throw err;
                console.log('saved');
            });
        }
    });
}

This is my code, when I console.log(embed); it outputs only the embed_urls of the API in the terminal just like I want, but when I write to the JSON it outputs "https://giphy.com/embed/XqOCxN1WpEzja"MsM0"" and that is all...

If I replace 'embed' with 'gif' in the fs.writeFile(), then of course it writes the entire API data to the JSON.

I have tried JSON.stringify(gif, undefined, 1), JSON.stringify(embed, undefined, 1).

I honestly searched and searched I hardly ever ask questions because I almost always find a solution but this one has me, I am pretty new to all this as well.

Brad Rappa
  • 11
  • 1
  • What is `embed`? An object? An array? What type is it? – vsenko Jun 23 '18 at 08:44
  • `embed` is my variable that I stuffed my data into from the `for` loop, finding anything that says `embed_url` and logging it. I figured out the issue.. What was happening is that using the `fs.writeFile()` I was creating a new file every time the loop happened, and writing to it, I needed to use `fs.appendFile()` so that it creates the file and then adds to the file instead of creating a new file every time. – Brad Rappa Jun 23 '18 at 18:57
  • Congratulations! – vsenko Jun 25 '18 at 05:28

0 Answers0