Read .json file

There is a frivolous comma after the last data pair.

Might be an encoding issue. Your JSON should be UTF-8 encoded.

Start with a hash in Ruby and have the JSON lib create the string and write it to the file path.

json = Hash[
  "use_defaut", "true",
  "skp_path", "skp path here",
  "stl_path", "stl path here"
].to_json

File.write("/file_path/file.json", json)

And then try to read it back into a hash with Ruby.

Lastly, it is not necessary to use a @var for the string read from the file as you’ll just convert it to a hash anyway. Instance variables are for persistence and access across the scope of an instance object.

You’ll be doing this file read within a method and the string reference from the file will go out of scope when the method ends, so it’ll automatically get “cleaned up” by Ruby’s garbage collector.

2 Likes