Sample Header Ad - 728x90

jq skip iteration over null

3 votes
1 answer
2645 views
I am using jq 1.7 # Data I am operating on info.json downloaded by yt-dlp
yt-dlp --write-info-json --skip-download https://www.youtube.com/watch?v=vlIO-7Rpi7c 
# JQ - Task - Now I want to remove all array inside subtitles whose name is not de, en-US and inside de, en-US select all objects with element ext == vtt. **This successfully works as follows**:
jq
jq 'pick(.subtitles | .de[],."en-US"[] | select (.ext == "vtt")) | del(..|nulls)' *.json
output
{
  "subtitles": {
    "de": [
      {
        "ext": "vtt",
        "url": "https://www.youtube.com/api/timedtext?v=vlIO-7Rpi7c&ei=JS05ZoT2Ftf0i9oP8dy4mAo&caps=asr&opi=112496729&xoaf=5&hl=en&ip=0.0.0.0&ipbits=0&expire=1715048341&sparams=ip%2Cipbits%2Cexpire%2Cv%2Cei%2Ccaps%2Copi%2Cxoaf&signature=AB2ECEEABF48D1A16ADC7ACCB8A072D63EE12DCC.5F56904251766FB68624A68FF2BBB57B9CCFFD2F&key=yt8&lang=de&fmt=vtt ",
        "name": "German"
      }
    ],
    "en-US": [
      {
        "ext": "vtt",
        "url": "https://www.youtube.com/api/timedtext?v=vlIO-7Rpi7c&ei=JS05ZoT2Ftf0i9oP8dy4mAo&caps=asr&opi=112496729&xoaf=5&hl=en&ip=0.0.0.0&ipbits=0&expire=1715048341&sparams=ip%2Cipbits%2Cexpire%2Cv%2Cei%2Ccaps%2Copi%2Cxoaf&signature=AB2ECEEABF48D1A16ADC7ACCB8A072D63EE12DCC.5F56904251766FB68624A68FF2BBB57B9CCFFD2F&key=yt8&lang=en-US&fmt=vtt ",
        "name": "English (United States)"
      }
    ]
  }
}
# Issue For some Youtube video, json file does not contain "en-US" but contain "en", while some contain all three of de, en, en-US. So I wrote the following
jq
jq 'pick(.subtitles | .de[],.en[],."en-US"[] | select (.ext == "vtt")) | del(..|nulls)' *.json
### Error jq: error (at :69): Cannot iterate over null (null) # Help required How to select only .ext == "vtt" for only language en,en-US,de if not all of these languages are present in the json file?
Asked by Porcupine (2156 rep)
May 6, 2024, 08:27 PM
Last activity: May 7, 2024, 12:39 PM