So I am hoping to convert a CSV file to JSON using Siri Shortcuts. I have a shortcut and a Pythonista script, but the Python script fails with the following error. The main shortcut pulls the CSV file, it is then sent to the “CSV to JSON…” shortcut (linked below), but I get this error in Pythonista. Any idea what’s going on.
Or alternatively, if you have an easier way to extract a JSON dictionary from a CSV file, preferably through Shortcuts, I am all ear (or eyes, I guess).
There are a few things wrong with your script and shortcuts, but even if you fix all of them, your setup still won’t work because Pythonista is thoroughly unreliable when used with Shortcuts. Every time I’ve tried (including trying to get your script and shortcuts working), I’ve come away frustrated.
There are two solutions:
Try to implement your entire automation within Pythonista. I don’t know if there’s an easy way to get a file picker in Pythonista.
Switch to Pyto as your Python. Pyto’s code editor is awful compared to Pythonista’s, and it doesn’t have as many iOS-specific modules, but when it comes to working with Shortcuts, it’s great. This will, of course, force you to buy a new app, and you may not want to do that if you don’t expect to do much in Python.
To make the data useful, you probably want to use the Get Dictionary From Input step. I ended with Show Results because I don’t really know what you want to do with the JSON.
The Python code, embedded in the Run Code step, is this:
import csv, sys, json
cvsString = sys.argv[1]
entries = []
csvReader = csv.DictReader(cvsString.splitlines())
for csvRow in csvReader:
entries.append(csvRow)
print(json.dumps(entries))
It takes the text from the CSV file you pick, converts it to JSON and prints it out. That becomes the input to the next Shortcuts step.
Now that I think of it, there’s probably a JavaScript library for converting CSV to JSON. If you find it, you could import it into Scriptable and use that. I prefer programming in Python.
Thank you for this. I think I can definitely work with this. I am good at Shortcuts, not so much at other languages. I have more experience with JavaScript. We will see how this goes.
I, too, prefer Python. And for this case - CSV - there is built in support.
I would echo that a JavaScript library for CSV might be helpful here. If it’s good enough. The Python one has lots of sophistication, particularly around dialects. Though I prescribe one dialect in my filterCSV project (not useful here).
Scriptable seems a good reason to veer towards JavaScript - if it’s capabilities are something you need.
(Off topic but I’m biased towards encouraging my university age daughter to learn Python ahead of JavaScript. The commercial market seems to want that and CSV support might be of real value to her.)
Hey Mark. I had also been looking for a way to convert csv to json within shortcuts and in fact I went down the same route with Pythonista. I went searching again after reading this thread and found GizmoPack which is kinda like Toolbox Pro and includes a “Parse CSV” action that does exactly what your looking for. In my case I’m parsing Airbnb reservation data and this little action has come to the rescue. Check it out