0

I got a json response w/ Python3 in Pycharm via an API from a web analytics platform. How do I save the json response in a file e.g. csv, json, text, etc.?

Input

reporting = getty.webtrekk_report()
print(reporting)

Output

{'calculationJobs': [{'type': 'TABLE', 'columns': [{'label': 'time_id_categories_t_year_month_day|0', 'values': ['20180901', '20180902', '20180903', ............
Sentino
  • 25
  • 4
  • `with open('output.json', 'wb') as fh: fh.write(self.report_normal)`? – Torxed Oct 24 '18 at 12:57
  • sorry, wrong code. the input would be `reporting = getty.webtrekk_report() print(reporting)` – Sentino Oct 24 '18 at 13:03
  • Still the same solution, `fh.write(reporting)` instead. A search for "how to write X to file in Python" would do the trick. JSON is nothing more than a string of letters, nothing fancy. – Torxed Oct 24 '18 at 13:09
  • Unfortunately, I get `TypeError: a bytes-like object is required, not 'dict'`. I also tried json.dump from other solutions, it doesn't create an error, but no file is being created – Sentino Oct 24 '18 at 13:30
  • Then do `open('output.json', 'w')`. Or `with open('output.json', 'w') as fh: json.dump(json_string, fh)`. Either way, it's possible. Check the manuals. – Torxed Oct 24 '18 at 15:13

0 Answers0