require ‘json’
s = %q!{:value => [ {“a” => nil, “b” => nil, “c” => 0}]}!
s # => “{:value => [ {“a” => nil, “b” => nil, “c” => 0}]}”
JSON.parse(s)
=>
~> from -:5:in `’
~>
/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/json/common.rb:155:in
`parse’: 757: unexpected token at ‘{:value => [ {“a” => nil, “b” => nil,
“c” => 0}]}’ (JSON::ParserError)
~> from
/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/json/common.rb:155:in
`parse’
~> from -:5:in `’
I have expected - {:value => [ {"a" => nil, "b" => nil, "c" => 0}]}
What the wrong I did here ?
The same with YAML#load
require ‘yaml’
s = %q!{:value => [ {“a” => nil, “b” => nil, “c” => 0}]}!
s # => “{:value => [ {“a” => nil, “b” => nil, “c” => 0}]}”
YAML.load(s)
=>
~>
/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych.rb:205:in
`parse’: (): did not find expected node content while parsing a
flow node at line 1 column 2 (Psych::SyntaxError)
~> from
/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych.rb:205:in
`parse_stream’
~> from
/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych.rb:153:in
`parse’
~> from
/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/psych.rb:129:in
`load’
~> from -:5:in `’
Your example string “s” is neither valid JSON or YAML. For starters, the
“=>” token is not allowed in either. You can’t use :value in JSON
either.
You need to use the double quoted string “value”
Tony A. wrote in post #1114104:
Your example string “s” is neither valid JSON or YAML. For starters, the
“=>” token is not allowed in either. You can’t use :value in JSON
either.
You need to use the double quoted string “value”
Sorry I couldn’t catch you. 
Ok, so you wanted to parse a Hash and it did not work. How about dumping
a
Hash out in what would be verifiable JSON and then comparing your test
string to the dumped Hash value?
When you have an error like this 99.9999999999% of the time it’s going
to
be you and not the parser that is incorrect.
John
On Mon, Jul 1, 2013 at 2:39 PM, Love U Ruby [email protected]
wrote:
“c” => 0}]}’ (JSON::ParserError)
Posted via http://www.ruby-forum.com/.
require ‘json’
require ‘yaml’
ruby = {:value => [ {“a” => nil, “b” => nil, “c” => 0}]}
JSON.dump ruby # => “{"value":[{"a":null,"b":null,"c":0}]}”
YAML.dump ruby # => “—\n:value:\n- a: \n b: \n c: 0\n”
Am 02.07.2013 10:10, schrieb Love U Ruby:
Actually I wanted the output {:value => [ {"a" => nil, "b" => nil, "c" => 0}]}
from “{:value => [ {“a” => nil, “b” => nil, “c” => 0}]}”
Please reread the previous answers…
Did you do that? No? Then do it now…
The point is, you feed strings that are neither valid JSON nor
valid YAML into the JSON / YAML parser. What do you expect?
That’s like putting buttons into a vending machine.
Josh was trying to show you what you should instead feed into
the parser, to get what you want.
BTW: JSON.parse is for parsing JSON. Not for getting some output.
Regards,
Marcus
unknown wrote in post #1114204:
Am 02.07.2013 10:10, schrieb Love U Ruby:
Did you do that? No? Then do it now…
Ok… Sorry I missed that actually. Apologies 
Josh C. wrote in post #1114121:
require ‘json’
require ‘yaml’
ruby = {:value => [ {“a” => nil, “b” => nil, “c” => 0}]}
JSON.dump ruby # => “{“value”:[{“a”:null,“b”:null,“c”:0}]}”
YAML.dump ruby # => “—\n:value:\n- a: \n b: \n c: 0\n”
Actually I wanted the output {:value => [ {"a" => nil, "b" => nil, "c" => 0}]}
from “{:value => [ {“a” => nil, “b” => nil, “c” => 0}]}”
Thanks