...

View Full Version : What's wrong with this JSON string?



kenatsun
04-24-2009, 03:58 AM
Here's the string:

{"bucket":"dhat-groups-3", "key":"teachings/00little01.txt", "etag":"0c44405e7efa6d576bc7809646418000"}

I'm getting an "invalid JSON string" message from the YAHOO.lang.JSON.parse() method. (See http://developer.yahoo.com/yui/docs/JSON.html) The full error message is:

Invalid JSON string returned from the success_action_redirect URL:
<pre>{"bucket":"dhat-groups-3", "key":"teachings/00little02.txt", "etag":"5592219e321dec57dbf34678a181839f"}</pre>

As far as I can tell, this string satisfies the syntax at http://www.json.org/.

Quoting the "/" in the "key" entry, so the string is...

{"bucket":"dhat-groups-3", "key":"teachings\/00little02.txt", "etag":"5592219e321dec57dbf34678a181839f"}

...gets the same error.

This is my first venture into JSON, so I hope there's something that my untrained eyes are missing.

~ TIA
~ Ken

bdl
04-24-2009, 04:43 AM
By my eye and by the results at jsonlint.com (http://www.jsonlint.com/), that's valid JSON. But, where does it come from? Is it being dynamically returned from a server-side process?

One thing to consider when returning JSON is the Content-Type of the returning object string.

kenatsun
04-24-2009, 04:54 AM
Thanks, bdl, for your quick response.

Yes, it's being returned from a server Python script.

I don't know if this matters, but I'm trying to adapt a PHP-based scheme for uploading to Amazon S3 so it works in my Python environment. The scheme is the one at http://developer.amazonwebservices.com/connect/entry.jspa?categoryID=188&externalID=1618.

~ Ken

rnd me
04-24-2009, 06:33 PM
does it work with eval? if yes, it looks like yahoo's parser needs more work.

i'll bet it based on the crockford JSON.parse, which i know has a one to two minor issues last i checked ( a while ago).

kenatsun
04-24-2009, 07:50 PM
I've found and (in a klunky way) solved the problem. When my server-side Python script returns the string, something somewhere is wrapping it in "<pre>" tags.

So the klunky solution was to add a line to the Javascript that strips these tags:

var responseText = responseText.substring('<pre>'.length, responseText.length - '</pre>'.length)

Of course, this solution collapses if that something somewhere ever decides to stop wrapping the string in <pre> tags. But that's a problem for another day.

rnd me
04-24-2009, 08:41 PM
Of course, this solution collapses if that something somewhere ever decides to stop wrapping the string in <pre> tags. But that's a problem for another day.

well, use replace instead of trim, and itt'l work with or without the pre tagsl:




var responseText = responseText.replace(/^\<pre\>,"").replace(/\<\/pre\>/, "");

kenatsun
04-25-2009, 12:51 AM
That's better. Had to tweak it a bit, thus:

responseText = responseText.replace(/^\<pre\>/, "").replace(/\<\/pre\>$/, "");

~ Thanks
~ Ken



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum