ruby write json file from an object

11,799

Solution 1

You'd need to add the commas yourself. f.write(n.to_json) is going to write out a single Province. It has no way to know you're going to keep writing more and need a comma.

Is there a reason you can't do this instead?

File.open('file_json_complete.json', 'w') do |f|
  f.puts prov_instances.to_json
end

Solution 2

I needed to take the hash {} and cast it to_json in order to create a json string

       class Province
         def to_json(*a)
           {'provOrigine' => @provOrigine.to_s, 
            'destination' => @destination.to_s, 
            'q1' => @q1.to_s, 'q2' => @q2.to_s, 
            'q3' => @q3.to_s, 'q4' => @q4.to_s
           }.to_json(*a)

         end
       end

I don't need to loop thought each instances. I could take the object Array and cast it to_json

File.open("file_json_complete.json", "w") do |f|
   f.write(prov_instances.to_json)
end
Share:
11,799
Papouche Guinslyzinho
Author by

Papouche Guinslyzinho

Updated on June 14, 2022

Comments

  • Papouche Guinslyzinho
    Papouche Guinslyzinho almost 2 years

    I'm getting an error expected ':' after property name in object at line 1 column 15 how can I get rid of '=>'? when I replace "=>" by ":" in the to_json methods I'm getting an error syntax error, unexpected ':', expecting =>

    require 'json'    
    class Province
            attr_accessor :provOrigine, :destination, :total, 
                            :q1, :q2, :q3, :q4
    
            def initialize(line)
                @provOrigine =  line.split(';').first.split(",").first
                @destination =  line.split(';').at(1).split(',').first
                @q1 = (line.split(';').at(4)).to_i
                @q2 = (line.split(';').at(5)).to_i
                @q3 = (line.split(';').at(6)).to_i
                @q4 = (line.split(';').at(7)).to_i
            end
            def to_json
              {'provOrigine' => @provOrigine.to_s, 'destination' => @destination.to_s, 'q1' => @q1.to_s, 'q2' => @q2.to_s, 'q3' => @q3.to_s, 'q4' => @q4.to_s}
            end
    
    
        end
    

    ... prov_instances = contains All the instances of Province ...

    File.open("file_json_complete.json", "w") do |f|
      prov_instances.each do |n|
        f.write(n.to_json)
      end
    end
    

    this is the result I'm getting

        {"provOrigine"=>"Alberta", "destination"=>"Terre-Neuve-et-Labrador", "q1"=>"777", "q2"=>"1089", "q3"=>"553", "q4"=>"474"}{"provOrigine"=>"Alberta", "destination"=>"Nunavut", "q1"=>"24", "q2"=>"70", "q3"=>"29", "q4"=>"29"}{"provOrigine"=>"Alberta", "destination"=>"Île-du-Prince-Édouard", "q1"=>"116", "q2"=>"69", "q3"=>"150", "q4"=>"64" 
    }
    

    and there is no commas between each object?

  • Papouche Guinslyzinho
    Papouche Guinslyzinho over 9 years
    Thanks Phillip but the output of your answer is "#<Province:0x8c40f10>","#<Province:0x8c352dc>" it's seems that I really need to append .to_json(*a) inside of the method to_json