JSON.parse string

Hello,
I just stumbled over the fact, that the JSON parser from ruby 1.9.2
allows only arrays and objects:

a = JSON.parse '"fooo"'
  JSON::ParserError: 710: unexpected token at '"fooo"'
a = JSON.parse "[\"a\"]"
  => ["a"]

As a workaround, I can wrap every text into an array and take the first
element of the result:

   JSON.parse( "[#{a}]" )[ 0 ]

But I wonder if this is the intended behavior and if so, why?

Kind regards

Torsten

···

--
Posted via http://www.ruby-forum.com/.

Because that's how proper JSON is formatted? The toplevel "item" must
be an array or an object. If it isn't, then you probably should be
using plain text insted of JSON.

-- Matma Rex

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.

Kind regards
Torsten

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

On Thu, Jul 19, 2012 at 11:36 AM, Torsten Robitzki <lists@ruby-forum.com> 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.

So I looked around. The RFC clearly states, in section 2:
[https://tools.ietf.org/html/rfc4627\]

   A JSON text is a serialized object or array.

      JSON-text = object / array

I think that "string".to_json is just an implementation quirk (there
is also 3.to_json etc.), which allows for prettier internal code –
#to_json on Array and Hash can be defined recursively.

-- Matma Rex

···

2012/7/19 Torsten Robitzki <lists@ruby-forum.com>:

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\""

Bartosz Dziewoński wrote in post #1069302:

So I looked around. The RFC clearly states, in section 2:
[https://tools.ietf.org/html/rfc4627\]

   A JSON text is a serialized object or array.

      JSON-text = object / array

Thanks Matma and Jesus , that helps. I find it to be a little odd, but
I'm sure there is a reason, why it is defined as such.

kind regards
Torsten

···

--
Posted via http://www.ruby-forum.com/\.