Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON data under parent named "data" #39

Closed
LachieKimber opened this issue Sep 25, 2017 · 1 comment
Closed

JSON data under parent named "data" #39

LachieKimber opened this issue Sep 25, 2017 · 1 comment

Comments

@LachieKimber
Copy link

Having an issue with getting the JSON data to delimit rows. I've tried reading through the documentation for jsonexport and had no luck.

Here is what the JSON data looks like when returned from an API:

{
   "data": [
      {
         "created_time": "2017-09-22T22:32:48+0000",
         "from": {
            "name": "CENSORED",
            "id": "529437227448196"
         },
         "message": "When you care to have the best service and a quality car visit Lexus of Albuquerque they are great. N H",
         "id": "10155842414888534_10155843734438534"
      },
      {
         "created_time": "2017-09-22T18:39:17+0000",
         "from": {
            "name": "CENSORED",
            "id": "134712363931856"
         },
         "message": "My grandson just upgraded to a Lexus and he says it is the best decision he ever made. He is excited that he doesn't have to worry about oil changes anymore.",
         "id": "10155842414888534_10155843129293534"
      }
   ],
   "paging": {
      "cursors": {
         "before": "NjQZD",
         "after": "NjMZD"
      },
      "next": "CENSORED"
   }
}

As a result of all the data being inside of "data": {} it seems to all be stuck on a single row when exported into CSV format.

Here is the CSV output:
https://s26.postimg.org/ywxrqvz3t/Screen_Shot_2017-09-25_at_10.41.08_AM.png

Here is my code:

var options = {
  mainPathItem: "data",
  verticalOutput: false,
  rowDelimiter: ','
};

request({
    url: url,
    json: true
}, function (error, response, body) {

    if (!error && response.statusCode === 200) {
        //console.log(body) // Print the json response
        var json_response = body;

        jsonexport(json_response, options,function(err, csv){
          if(err) return console.log(err);
          console.log(csv);
          fs.writeFile(`exports/${postID}.csv`, csv, function(err) {
           if (err) throw err;
           console.log('File successfully exported!');
          });
        });
    }
});

@LachieKimber
Copy link
Author

Fixed!

jsonexport(json_response.data, options,function(err, csv){

That line did the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant