shellwe
shellwe

Reputation: 115

Use JSON output from Flickr to display images from search

I need to display the images on my site from a JSON request.

I have the JSON:

https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=6a970fbb976a06193676f88ef2722cc8&text=sampletext&sort=relevance&privacy_filter=1&safe_search=1&per_page=5&page=1&format=json&nojsoncallback=1

And I have the format I need to put the photo URL in: https://www.flickr.com/services/api/misc.urls.html

But I don't know how I would loop through that, I found some examples similar, but I am still having trouble seeing what I need.

I am using JavaScript/jQuery to pull the info.

I figure I would have this in a loop.

CurrentPhotoUrl = 'https://farm'+CurrentPhotoFarm+'.staticflickr.com/'+CurrentPhotoServer+'/'+CurrentPhotoId+'_'+CurrentPhotoSecret+'_n.jpg'

But each of those variables would need to be populated with an value from the element. I would need to loop through all 5 elements that are in the JSON.

Any help on how to create this loop would be greatly appreciated.

Upvotes: 1

Views: 1431

Answers (3)

shellwe
shellwe

Reputation: 115

I'm not sure if this is the best solution but its is something someone suggested and it worked.

const requestURL = 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=6a970fbb976a06193676f88ef2722cc8&text=sampletext&sort=relevance&privacy_filter=1&safe_search=1&per_page=5&page=1&format=json&nojsoncallback=1'

$.ajax(requestURL)
  .done(function (data) {
    data.photos.photo.forEach(function (currentPhoto) {

      console.log('https://farm'+currentPhoto.farm+'.staticflickr.com/'+currentPhoto.server+'/'+currentPhoto.id+'_'+currentPhoto.secret+'_n.jpg')

    })
  })

Varun's solution worked for me as well. I don't know which one is better but I thought I would post this as well since it looks like they were done fairly differently.

Upvotes: 0

Zader
Zader

Reputation: 148

This answer assumes your json data will not change. So inside a .js file, set your json to a variable.

var json = <paste json here>;
// the photos are inside an array, so use forEach to iterate through them
json.photos.photo.forEach((photoObj) => {
    // the photo will render via an img dom node
    var img = document.createElement('img');
    var farmId = photoObj.farm;
    // you can fill out the rest of the variables ${} in the url
    // using the farm-id as an example
    img.src = `https://farm${farmId}.staticflickr.com/${serverId}/${id}_${secret}.jpg`
    // add the image to the dom
    document.body.appendChild(img);
}

Inside your html file that contains a basic html template, load this javascript file via a script tag, or just paste it inside a script tag.

If you want to get the json from the web page and assuming you have the jquery script loaded...

$.ajax({
        type: 'GET',
        url: <flicker_url_for_json>,
        success: (response) => {
            // iterate through json here
        },
        error: (error) => {
            console.log(error);
        }
    });

Upvotes: 1

Varun
Varun

Reputation: 1946

Try this code

var n = JSON.parse(x)  //x is the json returned from the url.
var _s = n.photos.photo;
for(var z = 0 ; z < n.photos.photo.length ; z++)
{
   var CurrentPhotoUrl = 'https://farm'+_s[z]['farm']+'.staticflickr.com/'+_s[z]['server']+'/'+_s[z]['id']+'_'+_s[z]['secret']+'_n.jpg'
   console.log(CurrentPhotoUrl);  
} 

Edit ( With actual JQUERY AJAX call )

    var n ='';
 $.ajax({url: "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=6a970fbb976a06193676f88ef2722cc8&text=sampletext&sort=relevance&privacy_filter=1&safe_search=1&per_page=5&page=1&format=json&nojsoncallback=1", success: function(result){

        console.log(result);
        n = result;
        var _s = n.photos.photo;
        for(var z = 0 ; z < n.photos.photo.length ; z++)
        {
           var CurrentPhotoUrl = 'https://farm'+_s[z]['farm']+'.staticflickr.com/'+_s[z]['server']+'/'+_s[z]['id']+'_'+_s[z]['secret']+'_n.jpg'
           console.log(CurrentPhotoUrl);  
        }  


    }});

Output:

https://farm8.staticflickr.com/7198/6847644027_ed69abc879_n.jpg https://farm3.staticflickr.com/2517/3905485164_84cb437a29_n.jpg https://farm1.staticflickr.com/292/32625991395_58d1f16cea_n.jpg https://farm9.staticflickr.com/8181/7909857670_a64e1dd2b2_n.jpg https://farm9.staticflickr.com/8143/7682898986_ec78701508_n.jpg

Upvotes: 1

Related Questions