Home > OS >  How to Filter JSON Files in Jinja Template?
How to Filter JSON Files in Jinja Template?

Time:10-06

i've been frustrated when filter JSON file, i new in JSON knowledge.

but i havesome JSON files imported from url, the code is like this :

{
   "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"
         }
      },

and then i put iteration (use {% for format in formats %} )in another file like this:

{% for format in formats %}   
                        
         {{format.format_name}}                          
                        
                    
 {% endfor %}

unfortunately, this iteration make show all format generated.

my question is, how can i filter only show by 'format_id' is "sb2" and 'format_id' is "sb1"> ?

Thanks

EDIT : how come i cannot say HI at the top of this thread? it always remove automatically

CodePudding user response:

Inside of your for loop you can use an if statment jinja if

{% for format in formats %}   
  {% if format.format_id in ["sb1", "sb2"] %}
         {{format.format_name}}                          
  {% endif %}                    
{% endfor %}

PS: In your JSON data, the format objects do not have a "format_name" key.

  • Related