How to Transform a get_request JSON Response to CSV for Google Drive Upload?

This topic was automatically generated from Slack. You can find the original thread here.

I need code to transform get_request response which is in json format, converted to Csv, and passed on the next step which is Google Drive upload media

To convert JSON data to CSV and upload it to Google Drive, you can use the following code as a starting point. Make sure you have connected your Google Drive account in the Pipedream UI. import { axios } from "@pipedream/platform"; import { google } from "googleapis"; import { parse } from "json2csv"; export default defineComponent({ props: { google: { type: "app", app: "google", }, url: { type: "string", label: "URL to make request to", }, }, async run({ steps, $ }) { // Fetch JSON data from the provided URL const response = await axios($, { method: "GET", url: this.url, }); // Convert JSON data to CSV const csvData = parse(response); // Initialize Google Drive API client const auth = new google.auth.GoogleAuth({ credentials: this.google.$auth.oauth_access_token, }); const drive = google.drive({ version: "v3", auth }); // Upload CSV data to Google Drive const fileMetadata = { name: "example.csv", mimeType: "application/vnd.google-apps.spreadsheet", }; const media = { mimeType: "text/csv", body: csvData, }; const result = await drive.files.create({ requestBody: fileMetadata, media: media, fields: "id", }); return result.data; }, }); Replace this.url with the URL of the API endpoint you want to fetch JSON data from. This code will convert the JSON data to CSV and upload it to your Google Drive as a new file named “example.csv”.

So i just need to transform it to CSV and pass it to the next step in workflow.

To transform JSON data to CSV and pass it to the next step in your workflow, you can use the json2csv package. Here’s an example of how to do this: import { parse } from "json2csv"; export default defineComponent({ async run({ steps }) { // Assuming the JSON data is in steps.get_request.data const jsonData = steps.get_request.data; // Convert JSON data to CSV const csvData = parse(jsonData); // Pass the CSV data to the next step return csvData; }, }); Replace steps.get_request.data with the actual step and property containing your JSON data. This code will convert the JSON data to CSV and pass it to the next step in your workflow.

assume this steps.get_request.$return_value

To transform the JSON data from steps.get_request.$return_value to CSV and pass it to the next step in your workflow, you can use the following code: import { parse } from "json2csv"; export default defineComponent({ async run({ steps }) { // Get JSON data from the previous step const jsonData = steps.get_request.$return_value; // Convert JSON data to CSV const csvData = parse(jsonData); // Pass the CSV data to the next step return csvData; }, }); This code will convert the JSON data from steps.get_request.$return_value to CSV and pass it to the next step in your workflow.

Convert “Email”,“Guest Secondary Emails”,“Telephone”,“Guest Secondary Phones”,“Guest Address”,“Booking Status”,“Rental”,“Arrive”,“Depart”,“Nights”,“Received”,“Checkin”,“Checkout”,“Booking ID”,“Inquiry ID”,“Source”,“Booked”,“Adults”,“Children”,“Currency”,“Total Cost”,“Base Rate”,“Tax”,“Booking Formula”,“Guest ID”
Christian Zaragoza,“zaragozachristian@gmail.com”,“secondaryemail@email.com”,“16462290993”,“”,“”,“Cancelled”,“”,“2018-09-13”,“2018-09-15”,“2”,“2018-09-11”,“”,“”,“15fabcda-b2ca-4193-8d6e-55b04ecf9aef”,“15fabcda-b2ca-4193-8d6e-55b04ecf9aef”,“tokeet”,“No”,1,0,“USD”,100.00,100.00,0.00,100,“a692ae87-a865-4917-a88e-97a0be74df8e”
Jodine,“jodine@tokeet.com”,“”,“”,“”,“”,“Booked”,“”,“2018-10-06”,“2018-10-08”,“2”,“2018-09-13”,“”,“”,“22633d53-5864-472c-bc92-b88da6b5223c”,“22633d53-5864-472c-bc92-b88da6b5223c”,“tokeet”,“Yes”,2,0,“USD”,350.00,350.00,0.00,350,“ae86fddb-3a57-4b41-bf35-1e680ce98aea”
Christian Zaragoza,“zaragozachristian@gmail.com”,“secondaryemail@email.com”,“16462290993”,“”,“”,“Cancelled”,“”,“2018-10-15”,“2018-10-20”,“5”,“2018-10-05”,“”,“”,“c4f116c6-8d06-4233-aba9-ba8e32c4a2ec”,“c4f116c6-8d06-4233-aba9-ba8e32c4a2ec”,“tokeet”,“No”,1,0,“USD”,4505.00,4505.00,0.00,4505,“a692ae87-a865-4917-a88e-97a0be74df8e” to csv file

To convert the given data to CSV and pass it to the next step in your workflow, you can use the following code: import { parse } from "json2csv"; export default defineComponent({ async run({ steps }) { // The given data as an array of objects const jsonData = [ { Email: "Christian Zaragoza", "Guest Secondary Emails": "[zaragozachristian@gmail.com](mailto:zaragozachristian@gmail.com)", Telephone: "16462290993", // ... add the rest of the data fields }, { Email: "Jodine", "Guest Secondary Emails": "[jodine@tokeet.com](mailto:jodine@tokeet.com)", Telephone: "", // ... add the rest of the data fields }, // ... add more objects if needed ]; // Convert JSON data to CSV const csvData = parse(jsonData); // Pass the CSV data to the next step return csvData; }, }); Replace the jsonData array with the actual data you want to convert to CSV. This code will convert the JSON data to CSV and pass it to the next step in your workflow.