jsonjqdumpyt-dlp

jq how to extract field only if exists for yt-dlp json dump


When i run the following:

yt-dlp --dump-json https://www.youtube.com/watch?v=nochapters | jq -cr '{ Date: .upload_date | (.[0:4] + "-" + .[4:6] + "-" + .[6:8]), ID: .id, Title: .fulltitle, Chaps: .chapters[] | {title} | join(", ") }'

it gives me an error:

jq: error (at :1): Cannot iterate over null (null)

How can extract the chapters from those videos that really have chapters? (because I think it gets stuck when no chapters are found).

Edit:

Input is like (in this example chapters exists):

{ "id": "pvkTC2xIbeY", "title": "How to Add Chapters to YouTube Videos | Chapters Explained", "formats": [ { "format_id": "sb2", "format_note": "storyboard", ... "comment_count": 2600, "chapters": [ { "start_time": 0, "title": "Intro", "end_time": 10 }, { "start_time": 10, "title": "YouTube Chapters", "end_time": 45 },... } }

Output like:

{"Date":"2020-06-17","ID":"pvkTC2xIbeY","Title":"How to Add Chapters to YouTube Videos | Chapters Explained","Chaps":"Intro, YouTube Chapters, What are YouTube chapters?, How to add chapter markers?, When are changes updated?, How to disable chapters?, Are chapters available in my country?"}


Solution

  • If .chapters is not present, it evaluates to null, and .chapters[] will fail with the error Cannot iterate over null (null).

    Depending on what you want as result value in this case, you could wrap the critical part in parentheses, use the error suppression operator ? to silence the error, and provide an alternative with the alternative operator //, e.g. (…)? // null.

    Note: .chapters[] | {title} | has("title") | join(", ") doesn't make sense even if there are chapters, as has returns a boolean. Do you want to concatenate the chapter titles, separated by commas, into a long string? Use .chapters | map(.title?) | join(", ") instead.

    jq -c '{
      Date: .upload_date | (.[0:4] + "-" + .[4:6] + "-" + .[6:8]),
      ID: .id,
      Title: .fulltitle,
      Chaps: ((.chapters | map(.title) | join(", "))? // null)
    }'