0

I'm using the Goo.gl URL shortener to shorten links. I sed it a Json string and it return results like this which gets stored in xmlHttp.response:

"{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/mR2d",
 "longUrl": "http://google.com/"
}"

After sending the code, I try to parse it using JSON:

 xmlHttp.send(jsonStr);
 var short_url = JSON.parse(xmlHttp.response).id ; 

When this code runs in a function, I get the following "Unexpected end of input" error:

getShortURL("http://google.com");
SyntaxError: Unexpected end of input
arguments: Array[0]
get message: function () { [native code] }
get stack: function () { [native code] }
set message: function () { [native code] }
set stack: function () { [native code] }
type: "unexpected_eos"
__proto__: Error

I had thought there may be some issue with the quotes in the string. What would be a good way to fix the problem?

Ari
  • 1,974
  • 19
  • 30
  • you mean :var short_url = JSON.parse(xmlHttp.response.id) ; does eval() work? – Sajjan Sarkar Nov 05 '12 at 21:42
  • 2
    There are not really quotes around the JSON, are they? Please post an actual response, I can't believe it's invalid JSON – Bergi Nov 05 '12 at 21:42
  • You can not do this with plain JavaScript since calling google's service requires a post and they do not support JSONP. – epascarello Nov 05 '12 at 21:45
  • Can you trim the beginning and ending quotes before parsing the JSON? – Joshua Dwire Nov 05 '12 at 21:47
  • @Bergi, maybe that's just how Chrome's console displays it, and there's another reason for the error. – Ari Nov 05 '12 at 21:52
  • 1
    Yeah, that's how console.log does it in the most browsers - they show quotes around to indicate it's a string, but don't escape the contents - they just print it as is, including linebreaks etc. That means your JSON is actually valid, could you please show us the entire error message? – Bergi Nov 05 '12 at 21:57
  • OK, I edited the question. It actually seems I can get something to work in the console when doing it one line at a time, but not when running a whole function. – Ari Nov 05 '12 at 22:05
  • Please print the stack trace and the whole message, not only the error object serialisation which only tells us there were getters for those... – Bergi Nov 05 '12 at 22:19
  • The most simple case works for me using JSONP. I'm not getting any errors: http://jsfiddle.net/mZTPn/. – pimvdb Nov 05 '12 at 22:22

2 Answers2

1

I think I realized what was wrong. The code tries to get the information from the XMLHttpRequest before it actually has anything. Need to wait until xmlHttp.onreadystatechange and xmlHttp.readyState == 4 and then get out the shortened URL. Then can send it where needed.

(Wasn't used to these kind of asynchronous issues...)

Ari
  • 1,974
  • 19
  • 30
0

See JSON: why are forward slashes escaped?

My guess is that maybe the JSON parser you're using is expecting forward slashes to be escaped. See if this input is accepted instead:

{
    "kind": "urlshortener#url",
    "id": "http:\/\/goo.gl\/mR2d",
    "longUrl": "http:\/\/google.com\/"
}
Community
  • 1
  • 1
Jacob
  • 77,566
  • 24
  • 149
  • 228