Question

Convert JSON to CSV with to CSV throwing an error


Userlevel 1
Badge

I’m following this tutorial to convert an input JSON to CSV with the code:

const lines = await JSON.parse(inputData.lines);
const csv = await toCSV(lines);
output = [{ csv }];

Which throws an error

ReferenceError: toCSV is not defined

 

The incoming payload is

[{"First Name":"First1","Last Name":"Last1","E-mail Address":"first@last1.com","Exam Name":"exam1"},{"First Name":"First2","Last Name":"Last2","E-mail Address":"first@last2.com","Exam Name":"exam2"},{"First Name":"First3","Last Name":"Last3","E-mail Address":"first@last3.com","Exam Name":"exam3"}]

When I remove the second line as done in this question, the output is transposed:

What I would like to get is a single output from the action with:

1
First Name: First1
Last Name: Last1
E-mail Address: first@last1.com
Exam Name: exam1
2
First Name: First2
Last Name: Last2
E-mail Address: first@last2.com
Exam Name: exam2
3
First Name: First3
Last Name: Last3
E-mail Address: first@last3.com
Exam Name: exam3

As is shown in the output of the action:

 


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

20 replies

Userlevel 7
Badge +9

Looks like you’re using the standard built-in Code by Zapier for this, rather than the custom app that @ikbelkirasan was sharing in that tutorial.  Try switching the app to the one they linked to in the post.  It’s an unpublised app so you’ll need to click that link to get access to use it.

Userlevel 1
Badge

@Zane Pardon my ignorance, how do I figure out the difference? I’ve tried the “Export to CSV in Advanced Utilities (2.1.0)” step as well, but all that did was convert JSON Data to csv… I don’t see an event named “Run JavaScript Code”. I thought I accepted the invitation to it, but don’t see it… 

Userlevel 7
Badge +9

The example and screenshot you posted is of a Zap configured using a completely different app - the Code by Zapier official built-in app. You can tell by the name of it, and by the logo. 

 

You’ve selected this one: https://zapier.com/apps/code/integrations. You’ll need to delete that step from your Zap and paste your code into a new Zap step using this one as the selected app : https://zapier.com/developer/public-invite/112461/039174f595539444ce5c14e114c1dc1e/ Note that it’ll have a red atom-looking logo and be called “Advanced Utilities”, not “Code by Zapier”

 

Looks like “Run JavaScript Code” in @ikbelkirasan’s app is actually a Search type step - perhaps that’s why you’re not seeing it where you expect it.  You might reach out to him and see if he’ll help you get up and running.

If that fails to work, we’re happy to help you as you write your own custom Zapier integration to do exactly what you need it to. https://zapier.com/platform

Userlevel 7
Badge +12

Hi @zap29550 - Let me know if you still need help setting this up. Like Zane said, you’ll need to select Advanced Utilities 2.1.0 after accepting the invite then look for Run JavaScript Code. You’ll then be able to paste the code and run it. It’s currently configured as a search step which was a mistake but I will change that in the next release.

Userlevel 1
Badge

@ikbelkirasan thanks that helped. 

@Zane for some reason it does not appear red atom-looking logo :( 

After running it, the output is a single line of CSV. It doesn’t match the nested-style output of the tutorial. 

 

Userlevel 1
Badge

@Zane for some reason my Advanced Utilities step does not have a red-atom looking logo

@ikbelkirasan I was able to run that code, but the output is a single line. I’m looking to get the same formatted result to pass into the next step as pictured above. Is that possible with this step?

Userlevel 7
Badge +12

@zap29550 - The output in the screenshot seems is not a single line, each item in the array is represented by a separate line. The whole text is stored in the csv text property.

Userlevel 1
Badge

@Zane update: the icon changed to a red-atom looking logo

@ikbelkirasan so I guess I have two questions: (1) can I format the csv to output each item as a separate line (2) can I ignore the header row

Userlevel 7
Badge +12

@zap29550 - Yes, that’s possible. Try the code snippet below:

const lines = JSON.parse(inputData.lines);
const csv = await toCSV(lines);
const [, ...outputLines] = csv.split("\n");

return [
{
csv: outputLines,
},
];

 

Userlevel 1
Badge

Thank you @ikbelkirasan 

I tried the code and the output of the step appears to break them out into separate lines but the subsequent step only takes it in as a single line (I tested an API call and e-mail). If I try to add another new line to the csv.split(“\n\n”); the step stops working. Also tried <br>, but no luck. Any suggestions

Userlevel 7
Badge +12

@zap29550 - You can only split by \n because that’s how the output is formatted. The output would be an array of strings.

Can you post a screenshot showing how you want to use the CSV lines in the subsequent step(s)? Thanks.

Userlevel 1
Badge

Got it. Something like this would be ideal:

https://uploads-us-west-2.insided.com/zapier-ca/attachment/0fdd5b39-ff34-4ed5-96a4-0a68906eb4ed.png

Userlevel 7
Badge +12

@zap29550 - If that’s the output you want then there’s no need to output CSV at all, you just need to parse the JSON and return it.

return [
{
lines: JSON.parse(inputData.lines),
},
];

If this does the trick then you can just use the Code by Zapier app in this case.

Userlevel 1
Badge

That creates n parameters to pass into the next step?

 

Userlevel 7
Badge +12

@zap29550 What’s the next step? It needs to support line items.

Userlevel 1
Badge

@ikbelkirasan it is Email by Zapier (send outbound email)

Userlevel 7
Badge +12

@zap29550 - In that case, you’ll need to add an intermediary Looping by Zapier step to execute the Email by Zapier multiple times, once per item.

 

 

 

 

Userlevel 1
Badge

Really sorry for any confusion; I’m looking for a single string/blob that is passed as a parameter to the next step. 

1
First Name: First1
Last Name: Last1
E-mail Address: first@last1.com
Exam Name: exam1
2
First Name: First2
Last Name: Last2
E-mail Address: first@last2.com
Exam Name: exam2
3
First Name: First3
Last Name: Last3
E-mail Address: first@last3.com
Exam Name: exam3

Would be a single strong/parameter… 

Please let me know if I can buy you a coffee 

Userlevel 1
Badge

This forum is being a PITA in responding, so let’s try again.

Thank you @ikbelkirasan 

My goal is to have a single parameter that is formatted as I showed in my original post (can’t repost as it’ll be sent into a moderation black hole) without having to loop. I just want to send one e-mail (for example) with n blocks.

 

Userlevel 7
Badge +10

@zap29550 
Just checking in to see if you still need help with this?