Search code examples
javascriptjqueryvimeo

parse nested JSON from vimeo API


I set up a fiddle, with a simplified json response. I need to iterate over the json, and check for an object that has a metadata.connections.live_video.status of 'streaming'

I can't seem to access the property and values even when I'm just trying to console.log the number of instances it is looping through multiple times. Ultimately, I need to check for this status and pull the entire object out (store it as const) so I can access other properties of it that I didn't include - but again only if its metadata.connections.live_video.status = 'streaming'

$.each(json.body.data, function(key , value){ // First Level
    $.each(value.metadata.connections, function(ky , vl ){  // The contents inside data
     console.log(ky);
   });    
});

Solution

  • https://jsfiddle.net/nrz3bu8y/

    let data = json.body.data.filter(function(value, key) {
      let connection = value.metadata.connections.live_video;
      return connection && connection.status === 'streaming';
    })
    

    You don't need to iterate connections, you know the key you're looking for. You also don't need to use jquery here, you're just working with vanilla javascript objects.