Reputation: 2367
The following two lines produce the same output:
1.9.3p327 :015 > {:key=>1234}.to_json
=> "{\"key\":1234}"
1.9.3p327 :016 > {"key"=>1234}.to_json
=> "{\"key\":1234}"
Which is the following json:
{
"key" : 1234
}
How can I get it to produce "{key:1234}"
?
Upvotes: 2
Views: 2934
Reputation: 935
The other poster is correct about it not being valid JSON. I just wanted to copy a ruby object from my console into my code using colons instead of hash rockets
In this context you can use: https://github.com/awesome-print/awesome_print
ap({key: 1234}, ruby19_syntax: true, index: false)
Upvotes: 2
Reputation: 369458
The output you want to achieve is not JSON. It is illegal. It seems kind of obvious that the JSON library will produce JSON output not something which is not JSON.
If you want to output something which is not JSON you need to use a library which is not JSON. In this particular case, it looks like you made up the output format, so you'll probably have to write the library yourself.
Upvotes: 3