Need Simple CSV Conversion to JSON in Siri Shortcuts using Pythonista (w/ sample data, failed python script)

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):
https://www.icloud.com/shortcuts/ea6672b220f741779090652d6cdb1052

Main Shortcut that Calls the above shortcut:

Here is the Python Code:

import csv, sys, json, clipboard,io,tempfile, webbrowser

CSVdata = sys.argv[1], sys.argv[2]
print('test ', CSVdata)

tmp = tempfile.TemporaryFile(mode='w+t',encoding='UTF-8')

tmp.write(CSVdata)
tmp.seek(0)
print(tmp.read())

#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)
    print(csvReader)
    for csvRow in csvReader:
        arr.append(csvRow)

#print(arr)

asText = json.dumps(arr)
print(asText)
clipboard.set(asText)

url = "shortcuts://"
webbrowser.open(url)

# 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:

  1. 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.

  2. 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[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.

1 Like

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.

1 Like

If you know JavaScript then I highly recommend the app Scriptable. Works great with Shortcuts.

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 :slight_smile:

1 Like