0

I know there have been a lot of questions about how to handle JSON and I have tried quite a few, but am really stuck.

What I am trying to achieve is the following:
I load a JSON and in that JSON, there is information about photos. Each photos has several attributes, one of them being the latitude and longitude.These have not been specified for all the photo's. I want to have a JSON object in the end which contains only the photo's with a lat/long value.

The JSON looks like this:

{
    "current_page": 1,
    "total_pages": 2,
    "total_items": 27,
    "photos": [{
                "id": 63151715,
                "user_id": 430997,
                "name": "Footsteps",
                "description": "Traces in the dunes",
                "camera": "Canon EOS 5D Mark II",
                "lens": null,
                "focal_length": "23",
                "iso": "50",
                "shutter_speed": "30",
                "aperture": "9",
                "times_viewed": 500,
                "rating": 44.2,
                "status": 1,
                "created_at": "2014-03-08T06:02:13-05:00",
                "category": 8,
                "location": null,
                "latitude": 52.399774296136734,
                "longitude": 4.558639526367187

And them some more attributes and photos. I have validated it and that seems ok.

var json = $.getJSON("https://api.500px.com/v1/photos?feature=user&username=bzwemmer&consumer_key=XXX",
    function (json) {
        console.log(json);
    });

var jobj = $.parseJSON(json);

for (var i = 0; i < jobj.length; i++) {
    var type = "point";
    // Path from json from Firebug = json.responseJSON.photos[0].latitude 
    var lat = json.responseJSON.photos[0].latitude;
    console.log(lat);
    if (lat !== null || lat !== "") {
        geoobj.photos.push({
            lat: photos.lat,
            lon: photos.lon,
            name: photos.name,
            image_url: photos.image_url
        });
    }
}

I want to select a couple of attributes and put them in another JSON for furter processing. However, with everything I try, I get the message in Firebug that lat = undefined.

I hope someone knows where I am going wrong, because I don't see it anymore.

3
  • Have you tried console.log(photos.lat); ? Commented Jun 28, 2014 at 15:05
  • You're not filtering JSON, you're filtering information in a JavaScript object. JSON exited the picture after the var jobj = $.parseJSON(json); line. Commented Jun 28, 2014 at 15:05
  • Thanks TJ, I realize my "programming vocabulary" is not what it should be Commented Jun 30, 2014 at 10:29

3 Answers 3

3

You are trying to access result outside of callback function. it will not work. always process result inside callback function. try following code.

<script type='text/javascript'>

    $.getJSON("https://api.500px.com/v1/photos?feature=user&username=bzwemmer&consumer_key=XXX", function(json) {

        var jobj = $.parseJSON(json);

        $(jobj.photos).each(function(ind, val){
            if(val.latitude != "" && val.longitude != "") {
                geoobj.photos.push({
                lat: val.latitude,
                lon: val.longitude,
                name: val.name,
                image_url: val.image_url});  
            }
        });
    });

</script>
Sign up to request clarification or add additional context in comments.

6 Comments

And use jobj rather than json.responseJSON
also I don't believe the test against "" is the right one. typeof val.latitude !== 'undefined' is probably more appropriate
Oh sorry i forget to remove OP's forloop
If you look at the response to OPs request, latitude and longitude can be null. Seems like it would be better to just do if(val.latitude && val.longitude)?
Also, I don't think you need to do $.parseJSON(json) - the response is already valid JSON, so you can just use it directly instead of jobj.
|
0

If you json is valid, it should work, check this jsfiddle sample,

Following is simplified version

var jsonObj={
         "current_page":1,
          "total_pages":2,
          "total_items":27,
          "photos":
               [{"latitude":52.399774296136734,
                 "longitude":4.558639526367187}]
         };

alert(jsonObj.photos[0].latitude);

Comments

0

You should accept Rashmin Javiya's answer. I started to answer the same way, but she beat me to it!

Here is a fixed version of your code that handles the pagination and writes some debug output.

var geoobj = {photos:[]};
var missingLLcounter = 0;
(function getPhotos(page) {
    var request = "https://api.500px.com/v1/photos?feature=user&username=bzwemmer&consumer_key=ByGjMRRao3aAozGsq6HuxZmJfA6ufWzNL32VPplp&page="+page;

    console.log('getting page '+page+': '+request);

    $.getJSON(request, function (json) {
        $(json.photos).each(function(ind, val) {
            if(val.latitude && val.longitude) {
                geoobj.photos.push({
                    lat: val.latitude,
                    lon: val.longitude,
                    name: val.name,
                    image_url: val.image_url
                });
            } else {
                missingLLcounter++;
            }
        });

        console.log('done with page '+page+', '+(json.total_pages-page)+' pages to go.');

        if(json.total_pages>page) {
            getPhotos(++page);
        } else {
            console.log('done! retrieved '+page+' page'+(page>1?'s':'')+' with '+(geoobj.photos.length+missingLLcounter)+' total records ('+missingLLcounter+' were missing lat/lngs).');
        }
    });
})(1);

1 Comment

Thanks, I accepted it. I'm just not every day in front of a PC for this stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.