Is it possible to parse JSON and extract all the filenames?

I’m trying to create a script which can process the output from a JSON web request and parse and extract all of the filenames (in this case video files) and finaly delete the files.

Here is a sample of the output:

[
{
"id": "17749",
"video_group_id": "videos-24e6654bfd1ab85bebb1f721a4be46e6fdb9ea8974d14442d3aaecd1f971fcbb",
"path": "/Volumes/Backup/Videos/Channels/Processed/YouTube/Tailosive EV - No Rivian Mini Truck? Telo Is ON IT!.mp4",
"title": "YouTube",
"video_title": "Tailosive EV - No Rivian Mini Truck? Telo Is ON IT!",
"image_url": "http://192.168.1.100:8089/dvr/uploads/83/content",
"thumbnail_url": "http://192.168.1.100:8089/dvr/files/17749/preview.jpg",
"duration": 365.692,
"playback_time": 0,
"tags": [
"HD",
"Stereo"
],
"categories": [
"Video"
],
"watched": true,
"favorited": false,
"delayed": false,
"cancelled": false,
"corrupted": false,
"completed": false,
"processed": true,
"locked": false,
"verified": false,
"last_watched_at": 1710372688817,
"created_at": 1710371353000,
"updated_at": 1710372688842
},
{

I duplicated your record so I'd have two values to test with, and this seems to work:

Download Macro(s): _JSON text.kmmacros (5.0 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 13.6.5
  • Keyboard Maestro v11.0.2

This doesn't go all the way to deleting the records, just shows you how to parse the JSON to extract the list of paths. Note that they're appended with a Return character after each, so each one winds up on its own row.

With all the paths in the variable, you could then do another For Each loop, looping through the lines in the variable, and deleting each one. (There are other ways to do the deletion that would potentially be faster but also riskier.)

-rob.

1 Like

Thank you so much! I got the web request working and alll looks good. However, I can’t figure out how to implement the delete command. Is there a fail safe I could add just to be safe during testing. I know I’m close, so if you would be so kind to show how I would create the delete loop, I would be very greatfull… :grinning:

Thank again so your help!

It should be as simple as a Get URL action, with the results saved to the JSON variable that you'll then use in the rest of the macro.

-rob.

The attached version of the macro includes a delete loop at the end. The loop includes a confirmation prompt that you'll have to say "Yes" to in order to delete each file.

Download Macro(s): _JSON text.kmmacros (6.3 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 13.6.5
  • Keyboard Maestro v11.0.2

-rob.

1 Like

You are a genius, my friend! It works like a charm. I can’t thank you enough for your help and your time…

With blessings,
Chuck :grinning:

1 Like

No problem, glad it worked for you!

-rob.