0

I need to convert a json to csv. I download with request library but I don´t know how to convert it

code:

import requests

print 'Fecha Inicio ej:2018-04-01'
start = raw_input()
print 'Fecha Fin ej:2018-04-01'
end = raw_input()
token = 'xxx'

url = 'https://publishers.com/api/2.0/stats.json?apitoken={}&begindate={}&enddate={}&metrics=Date;TotalImpression;Revenue'.format(token, start, end)
data = requests.get(url)
data = data.json()
print data
[{u'date': u'2018-04-01T00:00:00', u'totalImpression': 38, u'revenue': {u'currency': u'USD', u'value': 0.071443286340685}}, {u'date': u'2018-04-01T00:00:00', u'totalImpression': 27, u'revenue': {u'currency': u'USD', u'value': 0.61042972746864}}, {u'date': u'2018-04-01T00:00:00', u'totalImpression': 96, u'revenue': {u'currency': u'USD', u'value': 0.53916111627594}}, {u'date': u'2018-04-01T00:00:00', u'totalImpression': 23, u'revenue': {u'currency': u'USD', u'value': 0.928523654181515}}, {u'date': u'2018-04-01T00:00:00', u'totalImpression': 0, u'revenue': {u'currency': u'USD', u'value': 0}}, {u'date': u'2018-04-01T00:00:00', u'totalImpression': 58, u'revenue': {u'currency': u'USD', u'value': 0.28822054601461}}]
  • The two easiest ways are with `csv.DictWriter` and with `pandas`. – tdelaney May 07 '18 at 16:58
  • You'll have to flatten the data to make it columnar. For instance, revenue can have multiple currencies. You may need a column per expected currency. – tdelaney May 07 '18 at 17:03

0 Answers0