From: "Jesús Gabriel y Galán" Date: 2012-07-19T18:46:34+09:00 Subject: Re: JSON.parse string On Thu, Jul 19, 2012 at 11:36 AM, Torsten Robitzki wrote: > Bartosz Dziewoński wrote in post #1069285: >> Because that's how proper JSON is formatted? The toplevel "item" must >> be an array or an object. > > From json.org, I read that a json value can be a string, a number, an > object, an array, true, false or null. So I wonder, why "string" is > treated different from []. By the way, "string".to_json works as > expected: > > > "string".to_json > => "\"string\"" > >> If it isn't, then you probably should be using plain text insted of JSON. > > I'm implementing a protocol, where the payload is a JSON encoded value > and I think "string" is a valid JSON expression. From json.org: "JSON is built on two structures: A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence." and also: "A value can be a string in double quotes, or a number, or true or false or null, or an object or an array." So, as I understand it, the top level of a json has to be either and object (collection of key/*value* pairs) or an array (ordered list of *values*). The values in each of those are the ones that can be string, number, true, false, null, object or array. I think the to_json method transforms an object into a json value. Some values can be top level themselves (objects and arrays), while others have to be wrapped into one of those. I'm not quite sure about the exact behaviour of to_json, but that is what I would expect. Hope this helps, Jesus.