I’m trying to create a script which can process the output from a JSON web request “http://192.168.1.100:8089/api/v1/videos?watched=true” 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
},
{
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property ca : a reference to current application
set jsonString to "[
{
\"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 1\",
\"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
}
]"
set jsonString to ca's NSString's stringWithString:jsonString
set jsonData to jsonString's dataUsingEncoding:(ca's NSUTF8StringEncoding)
set jsonrecord to (ca's NSJSONSerialization's JSONObjectWithData:jsonData options:0 |error|:(missing value)) as record
The original JSON is a list.
Your better of not coercing to a record.
Keep it as a NSArray.
Then you can use theArray’s valueForKeyPath:”path”
Which will return an array of only the path from
Each item in the array. (Which the you can coerce to list if needed). And if there is only one item in the original JSON you only have one path in the array
Also you forgot to create the jsonData variable from the string
The following is a working example of technomorph’s suggestion.
use framework "Foundation"
use scripting additions
set theKey to "name" -- set to desired value
set theURL to "https://jsonplaceholder.typicode.com/users" -- set to desired value
set theURL to current application's |NSURL|'s URLWithString:theURL
set theData to current application's NSData's dataWithContentsOfURL:theURL
set theArray to (current application's NSJSONSerialization's JSONObjectWithData:theData options:0 |error|:(missing value))
return (theArray's valueForKey:theKey) as list
The following shortcut is functionally equivalent to the AppleScript in the preceding post, except that the returned values are written to a text file.