1

I'm writing an API that accesses bible passages from http://labs.bible.org/, and the JSON responses come back without a "response" header, or any hierarchy names. Firebug is showing that my GET requests come back as 200 status, but the response tab is always empty. If I type the url directly into the browser I get the results I want, but I don't know how to handle the JSON like that. Example: http://labs.bible.org/api/?passage=luke+9&formatting=full&type=json.

This is what the JSON looks like.

[
    {
      "bookname": "Luke",
        "chapter": "9",
        "verse": "1",
        "text": "<t /><p class=\"bodytext\">After<n id=\"1\" /> Jesus<n id=\"2\" /> called<n id=\"3\" /> the twelve<n id=\"4\" /> together, he gave them power and authority over all demons and to cure<n id=\"5\" /> diseases,",
        "title": "The Sending of the Twelve Apostles"
    },
    {
      "bookname": "Luke",
        "chapter": "9",
        "verse": "2",
        "text": "and he sent<n id=\"1\" /> them out to proclaim<n id=\"2\" /> the kingdom of God<n id=\"3\" /> and to heal the sick.<n id=\"4\" />"
    },
    {
        "bookname": "Luke",
        "chapter": "9",
        "verse": "3",
        "text": "He<n id=\"1\" /> said to them, “Take nothing for your<n id=\"2\" /> journey &#8211; no staff,<n id=\"3\" /> no bag,<n id=\"4\" /> no bread, no money, and do not take an extra tunic.<n id=\"5\" />"
    },
    {
        "bookname": "Luke",
        "chapter": "9",
        "verse": "4",
        "text": "Whatever<n id=\"1\" /> house you enter, stay there<n id=\"2\" /> until you leave the area.<n id=\"3\" />"
    },
    {
        "bookname": "Luke",
        "chapter": "9",
        "verse": "5",
        "text": "Wherever<n id=\"1\" /> they do not receive you,<n id=\"2\" /> as you leave that town,<n id=\"3\" /> shake the dust off<n id=\"4\" /> your feet as a testimony against them.”"
    },
    {
        "bookname": "Luke",
        "chapter": "9",
        "verse": "6",
        "text": "Then<n id=\"1\" /> they departed and went throughout<n id=\"2\" /> the villages, proclaiming the good news<n id=\"3\" /> and healing people everywhere.</p>"
    },
    {
        "bookname": "Luke",
        "chapter": "9",
        "verse": "7",
        "text": "<t /><p class=\"bodytext\">Now Herod<n id=\"1\" /> the tetrarch<n id=\"2\" /> heard about everything that was happening, and he was thoroughly perplexed,<n id=\"3\" /> because some people were saying that John<n id=\"4\" /> had been raised from the dead,",
        "title": "Herod&#8217;s Confusion about Jesus"
    },
   (...)
]

So how do I write the code to access and parse the JSON, and how would I cycle through all the results?

These are my functions for getting and parsing the JSON:

function callApi(argument, callBack){

        var requestUrl = apiUrl+argument+type;

        try{
            var request = new XMLHttpRequest();

            request.addEventListener("readystatechange",
            function() {callBack(request);}, false);

            request.open("GET", requestUrl, true);
            request.setRequestHeader("Accept", "application/json; charset=utf-8");
            request.send();
        }//end try
        catch(exception){
            alert("Request Failed");
        }//end catch
    }//end function callApi

function parseData(request){

    if(request.readyState==4 && request.status==200){
        var data = JSON.parse(request.responseText);
        displayNames(data);
    }//end if
}// end function parseData
  • 1
    Most languages have a JSON parser that will help you access the JSON elements. What language are you using? For java there is GSON, for Python and JavaScript there is built-in json support, for Objective-C there's JSONKit, for Perl there's a JSON package... etc – PaulProgrammer Apr 09 '13 at 17:19
  • Is this API being written in a certain programming language? – summea Apr 09 '13 at 17:20
  • 1
    I'm writing it in JavaScript. I know it has a JSON.parse() method, but I've always seen "response" at the top of the JSON, so I would access the different information like (data being the responseText) "data.response.bookname," and if there was more than one result, then I would set a loop to increment through them. I just don't see how I would do that here. – Jaylin Frederick Apr 09 '13 at 17:27

1 Answers1

0

What if you were to try using a different JSON URL like this: http://labs.bible.org/api/?passage=luke+9:3&type=json&callback=? (removing &formatting=full and adding &callback=? at the end.)

If you get the response without the formatting tags (all of the <HTML> tags) it seems to parse for me, at least in this example on jsFiddle: http://jsfiddle.net/L8Fed/2/

Update: Here is an example of how jQuery could be used here to grab the JSON from your URL:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function() {
    var json_url = "http://labs.bible.org/api/?passage=luke+9:3&type=json&callback=?";

    $.getJSON(json_url, function(json_response) { 
        for(var i = 0; i < json_response.length; i++) {
            alert(json_response[i].text);
        }
    });
});
</script>

Basically, this code:

  1. Includes jQuery
  2. Puts the JavaScript into the ready function (more details here...)
  3. Makes sure to call the API URL that includes the &callback=? at the end (for this reason)
  4. Calls jQuery's $.getJSON() function (that uses your API URL and gets the response back named json_response)
  5. Loops through the json_response and allows you to get whatever values you need. (And you'll probably want to take out the alert() function at some point... ;)
Community
  • 1
  • 1
summea
  • 7,390
  • 4
  • 32
  • 48
  • 1
    The code you posted is essentially what I'm trying to do. Not line for line, of course, but the same concept. I guess the real problem is that the response to my HTTP GET is returning as blank. I'll post my functions to retrieve and parse the data. – Jaylin Frederick Apr 09 '13 at 18:31
  • @JaylinFrederick Thanks for posting those functions; just wondering, but... is it at all possible (within the parameters of your project) to use jQuery instead of hand-coding the "GET JSON" part? – summea Apr 09 '13 at 19:37
  • 1
    Yes, it's totally possible, but the problem is that I'm not very familiar with jQuery. I'm still trying to learn about it. – Jaylin Frederick Apr 09 '13 at 19:46
  • @JaylinFrederick I've updated the answer from earlier to hopefully get it to a place that helps you better :) – summea Apr 09 '13 at 20:42
  • 1
    Thank you so much for your time. You've been such a big help to me. – Jaylin Frederick Apr 10 '13 at 04:03
  • @JaylinFrederick No problem; I hope it worked out! Looks like a cool project you are working on... – summea Apr 10 '13 at 05:55