โ09-25-2018 07:12 AM
Iโm brand new to Snaplogic and this is probably really easy - sorry in advance! But Iโve been stuck on this since Thursday, and itโs now Tuesdayโฆ please help me.
As a whole, I have this:
In more detail:
I have a JSON splitter, which has an output of the JSON I want to write to a CSV. And, if I preview the output in table view, it looks exactly like the CSV I want (I had an image here, but new users can only put one - sorry!). Previewing in JSON shows that itโs, well, JSON.
After that, I have a CSV formatter, but I donโt have anything configured in it. Should I? The output view for this is more or less empty, so this clearly isnโt correct. Iโve tried JSON formatters and a million other things here. I just donโt know.
Then I have my file writer (and as a bonus, I want only some of the fields from the JSON to be written to the CSV file).
I can do it in Python:
# Call the API
r = requests.get(url_to_call, headers = headers)
json_to_write = r.json()
# Open a file for writing
outfile = open(file_name,'w')
writer=csv.writer(outfile)
# Write CSV Header Row
writer.writerow(["Id", "FirstName", "LastName"])
# Loop through each line of the JSON; print key values needed
for x in json_to_write:
writer.writerow([x["Id"],
x["FirstName"],
x["LastName"]])
I found this post on turning JSON to CSV by @dmiller and this post on writing a Twitter Query to a file, but as far as I can tell, those are just โuse these snapsโ without showing how the snaps are configured, and Iโve tried those configurations of snaps.
Iโm so lost. Any help appreciated.
โ09-25-2018 11:41 AM
In your CSVFormatter, youโre using โ$Usernameโ as one of the header and the resulting output is a column of empty strings. I assume thatโs what youโre seeing to, yes? Note, that itโs blank because there is no property named โUsernameโ in the input document. There is a property named โuserIdโ, though.
So, if you change the header to โ$userIdโ, you should see data coming out of the CSVFormatter. If you want to change the name of the property, youโll need to use a Mapper snap, like so:
Iโm wondering if you were expecting the order of the list of headers in the CSVFormatter to correspond to the order of the columns in the preview? Is that right? Or, perhaps, Iโm reading too much into thisโฆ
โ09-25-2018 01:12 PM
Oh hmmmmm yes I understand. What was throwing me off is that if you validate the pipeline than at every snap, like after the JSON splitter, if you click the green output doc in the snap connector on the right it shows the correct output , but the CSV formatterโs green doc looks totally wrong (even after I fix the headers), so after it didnโt work the first time, I never checked the actual CSV file again:
Iโll keep a note to myself not to trust what the formatters display.
Thank you so much!! Youโre a hero.
โ09-25-2018 01:18 PM
Click where it says โPreviewโฆโ Youโre dealing with BINARY (diamond shape) data at this point, so the normal view doesnโt have the usual JSON data to show.
โ09-25-2018 01:21 PM
THATโS A LINK?!! Itโs gray and only underlined on hover and looks just like all the other (non-link) parts of the page; I didnโt even think to try. Oh my goodness. Thank you!!
โ09-25-2018 02:53 PM
Thanks for the feedback, a bug has been logged.