如何在Jinja模板中过滤JSON文件内指定format_id的格式
Jinja模板过滤指定format_id的格式内容
问题描述
现有如下JSON数据:
{"id":"_n2tEUURGiY","title":"I Just","formats":[{"format_id":"sb2","format_note":"storyboard","ext":"mhtml","protocol":"mhtml","acodec":"none","vcodec":"none","url":"","width":48,"height":27,"fps":0.45871559633027525,"rows":10,"columns":10,"fragments":[{"url":"","duration":218.0}],"audio_ext":"none","video_ext":"none","format":"sb2 - 48x27 (storyboard)"},{"format_id":"sb1","format_note":"storyboard","ext":"mhtml","protocol":"mhtml","acodec":"none","vcodec":"none","url":"","width":45,"height":45,"fps":0.5091743119266054,"rows":10,"columns":10,"fragments":[{"url":"","duration":196.39639639639637},{"url":"","duration":21.60360360360363}],"audio_ext":"none","video_ext":"none","format":"sb1 - 45x45 (storyboard)","resolution":"45x45","http_headers":{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4556.0 Safari/537.36","Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language":"en-us,en;q=0.5","Sec-Fetch-Mode":"navigate"}},{"asr":48000,"filesize":3772787,"format_id":"251","format_note":"medium","source_preference":-1,"fps":"None","audio_channels":2,"height":"None","quality":3,"has_drm":false,"tbr":138.348,"url":"","width":"None","language":"","language_preference":-1,"preference":"None","ext":"webm","vcodec":"none","acodec":"opus","dynamic_range":"None","abr":138.348,"downloader_options":{"http_chunk_size":10485760},"container":"webm_dash","protocol":"https","audio_ext":"webm","video_ext":"none","format":"251 - audio only (medium)","resolution":"audio only","http_headers":{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4556.0 Safari/537.36","Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language":"en-us,en;q=0.5","Sec-Fetch-Mode":"navigate"}},{"asr":44100,"filesize":"None","format_id":"18","format_note":"360p","source_preference":-1,"fps":25,"audio_channels":2,"height":360,"quality":6,"has_drm":false,"tbr":172.717,"url":"","width":360,"language":"","language_preference":-1,"preference":"None","ext":"mp4","vcodec":"avc1.42001E","acodec":"mp4a.40.2","dynamic_range":"SDR","protocol":"https","video_ext":"mp4","audio_ext":"none","vbr":172.717,"abr":0.0,"format":"18 - 360x360 (360p)","resolution":"360x360","filesize_approx":4819495,"http_headers":{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4556.0 Safari/537.36","Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language":"en-us,en;q=0.5","Sec-Fetch-Mode":"navigate"}}]}
当前使用的Jinja模板如下:
{% for format in formats %} {{format.format_name}} {% endfor %}
需要修改模板,使其仅输出format_id为"sb2"或"sb1"的格式内容。另外注意:原模板中的format.format_name字段在JSON数据中不存在,实际对应的是format.format字段,需一并修正。
解决方案
在Jinja的for循环中添加if条件判断,筛选出format_id属于指定列表的项即可。修改后的模板如下:
{% for format in formats %} {% if format.format_id in ['sb1', 'sb2'] %} {{ format.format }} {% endif %} {% endfor %}
说明
- 通过
format.format_id in ['sb1', 'sb2']判断当前格式的ID是否在目标列表中,仅符合条件的项才会被输出。 - 修正了字段名称:将原模板中的
format.format_name替换为JSON中实际存在的format.format,确保能正确渲染格式描述内容。
内容的提问来源于stack exchange,提问作者warstha
相关产品推荐
相关产品推荐

