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).
Sample CSV Data:
CSV to JSON Siri Shortcut (calls Pythonista script):
Main Shortcut that Calls the above shortcut:
Here is the Python Code:
import csv, sys, json, clipboard,io,tempfile, webbrowser
CSVdata = sys.argv, sys.argv
print('test ', CSVdata)
tmp = tempfile.TemporaryFile(mode='w+t',encoding='UTF-8')
#csvFilePath = "CSV Variable for Shortcuts.csv"
#jsonFilePath = "JSON Variable for Shortcut Dic.json"
arr = 
#read the csv and add the arr to an array
with open (tmp.name, 'rt', encoding='utf8') as csvFile:
csvReader = csv.DictReader(csvFile)
for csvRow in csvReader:
asText = json.dumps(arr)
url = "shortcuts://"
# write the data to a json file
#with open(jsonFilePath, "w") as jsonFile:
# jsonFile.write(json.dumps(arr, indent = 4))
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.
How easy is it Pyto to use with Shortcuts? Here’s a three-step shortcut that does what you want.
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
entries = 
csvReader = csv.DictReader(cvsString.splitlines())
for csvRow in csvReader:
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.
I, too, prefer Python. And for this case - CSV - there is built in support.
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