0

I'm trying to read a json url but JSON appearing some extra string before JSON start

JSON script is like:

throw 'allowIllegalResourceCall is false.';
{  
   "name":"mkyong",
   "age":30,
   "address":{  
      "streetAddress":"88 8nd Street",
      "city":"New York"
   },
   "phoneNumber":[  
      {  
         "type":"home",
         "number":"111 111-1111"
      },
      {  
         "type":"fax",
         "number":"222 222-2222"
      }
   ]
}

I'm trying to read this JSON using this below javascript:

<script>
$(document).ready(function() {           
    $('.button').click(function(){
    var ur= "http://json_url";
    alert(ur);
    alert(JSON.stringify($.getJSON(ur)));
    });
});
</script>

Is there any way to read this types of Json using java script/jQuery or ajax.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Khoyendra Pande
  • 1,627
  • 5
  • 25
  • 42

2 Answers2

3

I don't have enough points to add a comment on John Smith his answer, but he is on the right track, except from a little mistake probably because he is used to program in Python :-)

The regular expression should be: /^throw.*;\s*/ instead of "^throw.*;\s*"

Instead of using double quotes, use / at the beginning and end of the regular expression. That will do the trick.

Erik
  • 191
  • 5
  • 1
    But replace(/^throw.*;\s*/,"") will work once json is fetched, but $.getJSON(ur) is not working so how could we apply replace(/^throw.*;\s*/,"") method on json. – Khoyendra Pande Aug 27 '14 at 05:43
  • Retrieve the body as a string with $.get or $.ajax first, then strip the first sentence (with replace) and use $.parseJSON to convert the remaining string into a JSON object. – Erik Aug 27 '14 at 07:55
0

read this in some api doc

JSON Security String

GET requests are protected by including a security string on the line before the JSON text. The string generally looks like "throw 'allowIllegalResourceCall is false.';". To strip out this string, a regular expression should be used. For example, in python one could use:

json = re.sub(r"^throw.*;\s*","",json)

so you just need to replace the string with "" and use slashes around the regExp.

alert($.getJSON(ur).replace(/^throw.*;\s*/,"");

source : https://developers.jivesoftware.com/api/v3/cloud/rest/index.html

Erik
  • 191
  • 5
john Smith
  • 17,409
  • 11
  • 76
  • 117
  • 1
    I'm trying alert($.getJSON(ur).replace("^throw.*;\s*",""); but not getting any response/alert. – Khoyendra Pande Aug 26 '14 at 17:09
  • 1
    Is this for JSON Hijacking? Good god, that vulnerability only affected a *few* versions of Firefox, that are no longer supported anywhere. The regex will probably work fine, but tell the API developers to code to modern standards. – Katana314 Aug 26 '14 at 19:20
  • No Katana, not for JSON Hijacking I'm trying to use for my own site JSON and HTML where I'm calling both are in same domain. But I can't access Web service code, I can access JSON only. – Khoyendra Pande Aug 27 '14 at 05:41
  • Just a tiny remark, your "JSON" starting with "throw .." is not valid JSON. You can use [JSONLint](http://jsonlint.com/) to verify it. – Erik Aug 27 '14 at 08:41