Reputation: 3
I'm working with an API via python requests module which returns CSV formatted data. I don't want to write the data to a file since I'm trying to turn this process into a serverless function on GCP and it should be unnecessary to create a file just to be able to read it one line at a time. The data from the response in postman looks like this
"completed","email","role","first","last","title","company","AccountName","AccountNumber","City","Description","State","Street","Zip","agreementId"
"2019-07-04 10:14:28","[email protected]","SIGNER","Sam","Signerone","Title","","Sam,s Garage","654654","Cityville","Some description here, with commas.","CA","123 South Main","98673","CBJCHBCAABAAcvZGncvMUCUf9XqXr1fwCGmKctFn_qIS"
It may have more that one row of data but the header will always be the 1st row.
I'm able to split by \n
but am wondering what the best, most efficient method is to return this as JSON?
Upvotes: 0
Views: 39
Reputation: 1918
Try to use Pandas. First read the CSV-like string and then convert to JSON:
from io import StringIO
import pandas as pd
csv_data = StringIO("""
"completed","email","role","first","last","title","company","AccountName","AccountNumber","City","Description","State","Street","Zip","agreementId"
"2019-07-04 10:14:28","[email protected]","SIGNER","Sam","Signerone","Title","","Sam,s Garage","654654","Cityville","Some description here, with commas.","CA","123 South Main","98673","CBJCHBCAABAAcvZGncvMUCUf9XqXr1fwCGmKctFn_qIS"
""")
df = pd.read_csv(csv_data)
json_data = df.to_json(orient='records')
print(json_data)
# [{"completed":"2019-07-04 10:14:28","email":"[email protected]","role":"SIGNER","first":"Sam","last":"Signerone","title":"Title","company":null,"AccountName":"Sam,s Garage","AccountNumber":654654,"City":"Cityville","Description":"Some description here, with commas.","State":"CA","Street":"123 South Main","Zip":98673,"agreementId":"CBJCHBCAABAAcvZGncvMUCUf9XqXr1fwCGmKctFn_qIS"}]
Upvotes: 1