Get Lists or Dictionary from CSV Text

I occasionally need to get a list or lists, or a dictionary, from CSV text that contains two values per line. This shortcut gets one list:

One List from CSV.shortcut (21.9 KB)

The following shortcut gets two lists. The first Replace action returns a list of the text before the comma, and the second Replace action returns a list of the text after the comma.

Two Lists from CSV.shortcut (22.0 KB)

Getting a dictionary directly from comma- or tab-separated text is not possible, but this can be done with a JSON. My solution is kludgey, but I’ll work on this.

Dictionary from CSV.shortcut (22.3 KB)

I did not attempt to make a lists of lists, because it seems of little use in a shortcut.

I refined my CSV to Dictionary shortcut. It’s a bit shorter and does not use a repeat loop. It’s easily modified to work with TSV text.

CSV to Dictionary.shortcut (22.3 KB)

The following demonstrates making the JSON into a dictionary and getting a value from the dictionary.

All of the above shortcuts work only with CSV’s that have two values per line. For anything more complicated, a regular AppleScript should probably be used (here). However, if necessary, the above shortcuts can be edited to work, and the following is a simple example:

CSV to Lists.shortcut (22.9 KB)

I wanted to give this another try, and the shortcut included below creates a Dictionary from a CSV then retrieves information from the Dictionary using dot notation. A few comments:

  • As written, the shortcut only works with a CSV that contains three fields per record.
  • The shortcut might better (and more reliably) use regular expressions to parse the lines of the CSV.
  • I wrote this shortcut on a proof-of-concept basis only and is not intended for actual use.
  • The following screenshot only shows a portion of the shortcut.

CSV to Dictionary.shortcut (24.2 KB)

This shortcut is similar to that immediately above, differing in that it:

  • prompts the user to enter the desired information;
  • works with a CSV with any number fields in a record (but every record must have the same number of fields); and
  • creates a list of dictionaries instead of a list of dictionaries of dictionaries.

The following screenshot only shows a portion of the shortcut.

CSV to Dictionary.shortcut (25.7 KB)

This shortcut is a combination of the preceding two shortcuts. A few comments:

  • The shortcut will accommodate any number of fields in a CSV record, provided all records have the same number of fields.
  • The dictionary is a JSON that contains dictionaries in dictionaries. The first dictionary level is the person’s name, and the second dictionary level is the other data (i.e. age, location, and gender).
  • Because of the preceding, dot notation can be used to get the desired values.
  • This shortcut is of no practical use, but I’ve learned a lot about dictionaries and JSON.

CSV to Dictionary.shortcut (25.8 KB)

Just to finish this thread, I wrote a CSV to JSON Array shortcut and a CSV to JSON Object shortcut. With both shortcuts any number of CSV fields can be used. I ran timing tests for the CSV to JSON Array shortcut (but without the dialogs), and the result with 4 CSV records was about 100 milliseconds and with 31 CSV records was almost a full second.

The CSV to JSON Array shortcut:

CSV to JSON Array.shortcut (24.5 KB)

The CSV to JSON Object shortcut:

CSV to JSON Object.shortcut (24.8 KB)

I also wrote and tested a version of the CSV to JSON Array shortcut that only allowed three fields per record, and the timing results were 50 and 155 milliseconds for CSVs with 4 and 31 records.

CSV with Three Fields to JSON Array.shortcut (24.1 KB)

1 Like

I edited an earlier shortcut in this thread to work with CSV fields that contain commas and double quotes. Note should be made that even a minor error in the formatting of the CSV will cause the shortcut to return unexpected results. The timing results with 4 and 31 CSV records were 50 and 120 milliseconds.

CSV to JSON.shortcut (23.9 KB)

The following is a minor edit of the above, differing in that the shortcut replaces empty CSV fields in the JSON with an empty string. As written, the first field cannot be empty, although this can be changed. I’ve also cleaned up the regex pattern a bit.

CSV to JSON.shortcut (24.2 KB)