pat310 / google-trends-api

An API layer on top of google trends
https://www.npmjs.com/package/google-trends-api
MIT License
874 stars 174 forks source link

How to parse output in JSON #170

Closed chriszlr closed 10 months ago

chriszlr commented 1 year ago

when i execute this code:

  googleTrends
    .interestOverTime({ keyword: "Women's march" })
    .then(function(results) {
      res.status(200).json(results);
    })
    .catch(function(err) {
      console.error("Oh no there was an error", err);
    });

I want the output to be in a readable JSON format, but instead i get something like this:

[{\"time\":\"1072915200\",\"formattedTime\":\"Jan 2004\",\"formattedAxisTime\":\"Jan 1, 2004\",\"value\":[1],\"hasData\":[true],\"formattedValue\":[\"1\"]},{\"time\":\"1075593600\",\"formattedTime\":\"Feb 2004\",\"formattedAxisTime\":\"Feb 1, 2004\",\"value\":[1],\"hasData\":[true],\"formattedValue\":[\"1\"]}, ....
chriszlr commented 1 year ago

fixed it,

googleTrends
    .interestOverTime({ keyword: "Women's march" })
    .then(function(results) {
      const jsonResponse = JSON.parse(JSON.parse(JSON.stringify(results)));
      // Do something with the `jsonResponse` object
      res.status(200).json(jsonResponse);
    })
    .catch(function(err) {
      console.error("Oh no there was an error", err);
    });

AI isnt bad at all