SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    792
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Json doesn't validate

    why does this JSON not validate in JSON Lint?

    Code:
    callback({
    "stores": [
    {
    "name": "sites",
    "content": [
    {
    "link_text": "link",
    "link_src": "http://www.store1.com/"
    },
    {
    "link_text": "link",
    "link_src": "http://www.store2.com/"
    },
    {
    "link_text": "link",
    "link_src": "http://www.store3.com/"
    },
    {
    "link_text": "link",
    "link_src": "http://www.store4.com/"
    },
    {
    "link_text": "link",
    "link_src": "http://www.store5.com/"
    },
    {
    "link_text": "link",
    "link_src": "http://www.store6.com/"
    }
    ]
    }
    ]
    });

    thank you....
    Last edited by spikeZ; Jun 30, 2013 at 01:04.

  2. #2
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,267
    Mentioned
    33 Post(s)
    Tagged
    5 Thread(s)
    Because it's wrapped in a callback, which is valid JS but not JSON:
    Code JavaScript:
    callback(
      // Valid JSON
    );
    If you strip away the callback code, everything else validates correctly.

  3. #3
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    792
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ok.. thank you very much..

    I still find JSON so confusing... I wish we were all still just using XML....;-)

    so: is this "callback" thingie relevant for when I pull this JSON with jQuery/AJAX?

    thank you....

  4. #4
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,267
    Mentioned
    33 Post(s)
    Tagged
    5 Thread(s)
    You don't need the wrapper for normal JSON, but if you want to request data from a different domain to the one your JS is running from, you have to use what's called JSONP. It's a way of getting around the same origin policy. There's a good explanation over at wikipedia: http://en.wikipedia.org/wiki/JSONP

  5. #5
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    792
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ok, so JSONP is only for when you're pulling from a remote domain?

    that "callback" thingie is only necessary for JSON feeds that will be accessed remotely?

    I've dabbed a bit in pulling from public JSON feeds..... for example...

    http://api.flickr.com/services/feeds...jsoncallback=?

    it's wrapped in parenthesis, is this what you mean? (it doesn't have the world "callback", but it looks like it's the same thing.. is this correct?)

    thank you...

  6. #6
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,267
    Mentioned
    33 Post(s)
    Tagged
    5 Thread(s)
    Yeah, you can call the function whatever you want.. some people name it callback, but that's just a matter of preference. In the flickr URL you gave, you can change the name of the callback function by changing this part of the query string: &jsoncallback=foobar (try this and you'll see that the returned JSON is wrapped in a call to the function foobar).

  7. #7
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    792
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    actually, this public JSON,

    http://api.flickr.com/services/feeds...ny&format=json

    is not wrapped in a "callback" thingie... it's not wrapped in parenthesis.....

    so I guess I'm confusing the "callback" that's part of JSON address url (when you call it with JSONP) and this "callback" word that, in some cases, I guess, wraps public JSON feeds.......

    thank you.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •