Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7413813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:57:40+00:00 2026-05-29T06:57:40+00:00

I have a json file. I am using it to store information, and as

  • 0

I have a json file. I am using it to store information, and as such it is constantly going to be both read and written.

I am completely new to ruby and oop in general, so I am sure I am going about this in a crazy way.

class Load
    def initialize(save_name)
    puts "loading " + save_name
        @data = JSON.parse(IO.read( $user_library + save_name ))
        @subject = @data["subject"]
        @id = @data["id"]
        @save_name = @data["save_name"]
        @listA = @data["listA"] # is an array containing dictionaries
        @listB = @data["listB"] # is an array containing dictionaries
        
    end
    attr_reader :data, :subject, :id, :save_name, :listA, :listB
end
 
example = Load.new("test.json")
puts example.id

=> 937489327389749

So I can now easily read the json file, but how could I write back to the file – refering to example? say I wanted to change the id example.id.change(7129371289)… or add dictionaries to lists A and B… Is this possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:57:41+00:00Added an answer on May 29, 2026 at 6:57 am

    The simplest way to go to/from JSON is to just use the JSON library to transform your data as appropriate:

    • json = my_object.to_json — method on the specific object to create a JSON string.
    • json = JSON.generate(my_object) — create JSON string from object.
    • JSON.dump(my_object, someIO) — create a JSON string and write to a file.
    • my_object = JSON.parse(json) — create a Ruby object from a JSON string.
    • my_object = JSON.load(someIO) — create a Ruby object from a file.

    Taken from this answer to another of your questions.

    However, you could wrap this in a class if you wanted:

    class JSONHash
      require 'json'
      def self.from(file)
        self.new.load(file)
      end
      def initialize(h={})
        @h=h
      end
    
      # Save this to disk, optionally specifying a new location
      def save(file=nil)
        @file = file if file
        File.open(@file,'w'){ |f| JSON.dump(@h, f) }
        self
      end
    
      # Discard all changes to the hash and replace with the information on disk
      def reload(file=nil)
        @file = file if file
        @h = JSON.parse(IO.read(@file))
        self
      end
    
      # Let our internal hash handle most methods, returning what it likes
      def method_missing(*a,&b)
        @h.send(*a,&b)
      end
    
      # But these methods normally return a Hash, so we re-wrap them in our class
      %w[ invert merge select ].each do |m|
        class_eval <<-ENDMETHOD
          def #{m}(*a,&b)
            self.class.new @h.send(#{m.inspect},*a,&b)
          end
        ENDMETHOD
      end
    
      def to_json
        @h.to_json
      end
    
    end
    

    The above behaves just like a hash, but you can use foo = JSONHash.from("foo.json") to load from disk, modify that hash as you would normally, and then just foo.save when you want to save out to disk.

    Or, if you don’t have a file on disk to begin with:

    foo = JSONHash.new a:42, b:17, c:"whatever initial values you want"
    foo.save 'foo.json'
    # keep modifying foo
    foo[:bar] = 52
    f.save # saves to the last saved location
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an AS 3.0 class that loads a JSON file in using a
i am trying to select an item using Spinners. I have a JSON file
So, I have a json file and I want to get the names of
I have the following json code file named: sections.json { section1: { priority: 1,
I have a web service that queries data from this json file, but I
I have a configuration file in the following JSON format: { key1: value1, key2:
I have a php file somejson.php that echos a json encoded array {jsonone:first json,jsontwo:second
I have this swf (flash) file that provides the json that needs to be
I have an external file in php, outputed as json format I want the
I have a JSON array with ActiveRecord objects. These objects can be reconstructed using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.