Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

How can I generate JSON instead of YAML file?

Hi, I saw that there is an option also for writing json, and I am successfully creating the file. However the content is just an array with one string inside. I did file.write(contacts.to_json) but this seems like its not doing the thing :)

def save File.open("contacts.json", "w") do |file| file.write(contacts.to_yaml) end end

2 Answers

Ari Misha
Ari Misha
19,323 Points

First off, json is a literally a string formatted as hash or JS objects. Just coz they look like JS objects, doesnt mean you can perform same methods on json. First, you're gonna have load json into a hash or vice-versa. There is a method in python, called "dumps" , which pretty much dumps your python code into json. There is a similar method in Ruby as well, its called "dump"(not dumps). It dumps an obj into json string. Check out the syntax for dump Here.

And there is a method called "loads" in python, which is opposite of "dumps". It loads json into python code. Similar method exists in Ruby called "load", which does same thing as "loads" in Python. Check out the syntax Here

Hope this might help ya! (:

Ari Misha
Ari Misha
19,323 Points

Hiya Zlatko! Is your file a simple Ruby file or a method in Rails class? Here im going to assume that this a simple Ruby file. So you're opening this file named "contacts.json" in "write" mode. Two things you need to consider here:

  • Writing an existing file will overwrite the existing content that existed before over writing.

  • You're opening a json file , not a ".rb" file.

In the second code, in the code block, you've already selected the file "contacts" so you dont have to addtionally select it in order to write to it. Here is the syntax for opening a file in "write" mode:

# notice "your text" is a string and we are over writing with "your text" in "yourfile"
File.open(yourfile, 'w') { |file| file.write("your text") }

One other thing, in order to dump your file to json in ruby, first you need to "require" json module in your Ruby file and then the file is eligible to load as a json file. Here is the example:

require json

File.open(yourfile, 'w') { |file| file.write("your text").to_json }

OR

File.open(yourfile, 'w') { |file| file.write("your text").to_yaml }

Hi Ari Misha ,

Yes it is a simple ruby file. I have tried the .to_json version. But it seems like it doesn't parse it correctly. I have something like ["my text"], and I want to achieve something like [ "key": "my text" ], maybe I need to do something similar to javascript's JSON.stringify() ?!?