Take the 2-minute tour ×

I wonder why i cant decode this json string, all what i want is to convert this to a proper Ruby hash, anyone have an idea? i think the array of objects is cracking it ?


{"{\"origins\":"=>{"{\"origin\":\"this\"},{\"origin\":\"dont\"},{\"origin\":\"dont me please\"},{\"origin\":\"and me please\"},{\"origin\":\"dont\"},{\"origin\":\"dont\"},{\"origin\":\"dont\"},{\"origin\":\"okay\"},{\"origin\":\"dont\"},{\"origin\":\"go\"},{\"origin\":\"go\"}"=>{",\"url\":\"file:///Users/waheed/Desktop/untitled.html\",\"apik\":\"helloapik\",\"host\":\"http://localhost:3000/\"}"=>nil}}}

now in my javascript im doing this

//this is the object im trying to send over xmlhttprequest and im using JSON.org library which has the stringify method

 function tObject(origins,url,apik){
    this.origins=origins; //this is an array of string

    var t = new tObject(myStringArr,"www.foo.com","welcome guys");

ajax = new Ajax(); //this is an xhcon class you dont worry about it url here http://xkr.us/code/javascript/XHConn/
ajax.connect("http://localhost:3000/","POST",JSON.stringify(t), callback); 

in my rails app the parameters that has been posted looks like this:

Parameters: {"{\"origins\":"=>{"{\"origin\":\"this\"},{\"origin\":\"yo yo\"},{\"origin\":\" me please\"},{\"origin\":\"and me please\"},{\"origin\":\"here\"},{\"origin\":\"and again\"},{\"origin\":\"again\"},{\"origin\":\"okay\"},{\"origin\":\"yes\"},{\"origin\":\"go\"},{\"origin\":\"go\"}"=>{",\"url\":\"www.foo.com\",\"apik\":\"welcome guys\"}"=>nil}}}

why it results with nil at the last ? i've tried to decode it but it could not work because it blame the string is not json string ?!!?

TIA, waheedi

share|improve this question
Where did you get the string from above? Is it an "inspected" string from the console? Or a RAILS_DEFAULT_LOGGER.info(params.to_s) ? –  Matt Feb 16 '10 at 15:45
this is what rails default logger logs on console when, a request happen –  Waheedi Feb 16 '10 at 15:59
I think the trouble here is that isn't a string, and it can't be parsed. It looks kind of like JSON, but only vaguely, because a lot of the formatting is off. It's a Hash with nested Hashes and a lot of them contain incorrect keys or values like '{"origins":' which is incomplete by itself. I'm not sure where you're getting this from, but something has half-parsed it and left it in this broken state. –  tadman Feb 16 '10 at 16:11
If this is the result of a post, do you have access to the raw data before it is parsed? Maybe it is being sent in with some encoding problems. What you see in the Rails log is the result of an attempted parse. You can get this out of FireBug more often than not if submitting from a browser. –  tadman Feb 16 '10 at 16:12
It looks like you have an actual Ruby hash there, not JSON. They look very similar, but are quite different. JSON does not have =>, or nil. –  Matt Feb 16 '10 at 16:24
show 3 more comments

2 Answers

Maybe you pasted it badly because I'm getting this error even trying to load that string:

syntax error, unexpected tASSOC, expecting $end
s = "{\"origins\":"=>{"{\"origin\":\"this\"},{\"ori...
share|improve this answer
sorry for that, i've copied it from console as it is now, –  Waheedi Feb 16 '10 at 15:16
add comment

That's not a valid JSON string. Both nil and => are invalid values for JSON.

share|improve this answer
i've updated my post, im really confused what is causing this?? –  Waheedi Feb 16 '10 at 16:31
add comment

Your Answer


