0

I want to feed this JSON URL to another site

http://naturetrek.co.uk/blog/api/get_recent_posts/

But the problem is that because of the initial three <p> tags its not valid JSON, you can check this in here...

http://jsonlint.com/

if u remove these 3 tags, it becomes valid.

We cant change the JSON string output from the Naturetrek Blog.

In our own code we use the following....

<h2>Naturetrek BLOG</h2>
<div id="ntblog"></div>

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">

    $(document).ready(function () {

        var blogURL = "http://naturetrek.co.uk/blog/api/get_recent_posts/";

        $.getJSON(blogURL, function(info) {

            alert("here");
            var output = info.status;

            /*
            for (var i = 0; i <= info.posts.length - 1; i++) {
            output += '<li>' +
            '<a href = "' + info.posts[i].url +
            '">' + info.posts[i].title + '</a></li>';

            }
            */

            var ntblog = document.getElementById('ntblog');
            ntblog.innerHTML = output;
        });
    });
</script>

The alert never gets called, because the JSON is invalid JSON. MY QUESTION IS -- IS THERE A WAY TO PREPARSE THE JSON TO REMOVE THE THREE <p> TAGS, SOMEHOW? OR ANOTHER WAY U CAN THINK OF?

tripleee
  • 175,061
  • 34
  • 275
  • 318

2 Answers2

2

$.getJSON is just shorthand for:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

See documentation here: http://api.jquery.com/jQuery.getJSON/

Just use $.ajax change dataType to "html" and parse your modified JSON data in the success function with $.parseJSON

Note: Since it seems you are doing a cross-domain request, you must use jsonp, read more here:

http://learn.jquery.com/ajax/working-with-jsonp/

mpaf
  • 6,597
  • 6
  • 38
  • 42
  • Thanks mpaf, Im sorry Im really no expert at this - how do I call that .ajax function, get the reuslt into a string, strip the first three

    tags then parse the resultant JSON string, bearing in mind its not valid html either

    – WebInspired Dec 03 '13 at 11:52
1

You should REALLY ask them to fix their JSON...

In the meantime, as a patch, you should download it as HTML content, preparse it (by removing the tags) and then parse the resulting content as plain JSON.

But the above procedure is really a patch that should disappear when the JSON producers fixes it.

Giupo
  • 413
  • 2
  • 9
  • Getting them to fix it is NOT an option, how would I go about downloading it as HTML content, do you mean via Javascript calls? – WebInspired Dec 02 '13 at 10:07
  • 1
    exactly what @mpaf replied :). get the HTML via `$.ajax` and then modify the poorly written JSON and the result of the last operation goes straight to `$.parseJSON` – Giupo Dec 02 '13 at 13:19