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

  • SEARCH
  • Home
  • 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 8453841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:51:55+00:00 2026-06-10T11:51:55+00:00

I have a problem with ruby and json. I have a json-file with information

  • 0

I have a problem with ruby and json.
I have a json-file with information scraped from the internet. For the following problem I’ll use a hardcoded file, which has the following syntax:

[{
  "day": "20120827_234558",
  "entries": [
    {
      "rank": "3",
      "club": "SuS Schalke 1896 e.V.",
      "votes": "126"
    },
    {
      "rank": "4",
      "club": "TuS Hamborn-Neumühl 07 e.V.",
      "votes": "120"
    }
]
},{
  "day": "20120827_234700",
  "entries": [
    {
      "rank": "1",
      "club": "TLV Germania 1901 Essen-Überruhr",
      "votes": "210"
    },
    {
      "rank": "2",
      "club": "Rumelner TV",
      "votes": "141"
    }
]
}]

I wrote then a ruby script which loads the json from the file and puts it into a hash,
fetches some information from the internet (as well hard coded in this example), adds these new information to the hash, converts the hash to json and stores it in the file again.

require 'rubygems'
require 'open-uri'
require 'json'

fname = 'ranking.json'

json = JSON.load(File.open(fname))

json.each do |ranking|
  puts 'entry:'
  puts ranking['day']
end

puts "\n";

new_data = Array.new
new_data = { "day" => "20120828_234558", "entries" => "sgankhask" }
json << new_data.to_json

json.each do |ranking|
  puts 'entry:'
  puts ranking['day']
end

So it’s all about simply appending data in json format to an already existing json.

But if I execute this script, I get the following output:

entry:
20120827_234558
entry:
20120827_234700

entry:
20120827_234558
entry:
20120827_234700
entry:
day

I’m confused about the last row. I assumed the last row to be entry: 20120828_234558.
It seems, that Ruby takes the key (‘day’) of the hash instead of the value (‘20120828_234558’)?

What is wrong in my script? Any help is appreciated.

  • 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-06-10T11:51:56+00:00Added an answer on June 10, 2026 at 11:51 am

    JSON is a string format for representing data structures. In Ruby, those data structures are represented with hashes (sometimes called dictionaries or maps) and arrays and so forth.

    The last item you add, new_data, you invoke to_json on it first, turning it into a string in the JSON format. So your data looks like this:

    [{"day"=>"20120827_234558",
      "entries"=>
       [{"rank"=>"3", "club"=>"SuS Schalke 1896 e.V.", "votes"=>"126"},
        {"rank"=>"4", "club"=>"TuS Hamborn-Neumühl 07 e.V.", "votes"=>"120"}]},
     {"day"=>"20120827_234700",
      "entries"=>
       [{"rank"=>"1", "club"=>"TLV Germania 1901 Essen-Überruhr", "votes"=>"210"},
        {"rank"=>"2", "club"=>"Rumelner TV", "votes"=>"141"}]},
     "{\"day\":\"20120828_234558\",\"entries\":\"sgankhask\"}"]
    

    See how the last item is actually a string, not a hash? So when you say ranking['day'], it is not asking a hash for its key 'day', but rather asking the string for the substring 'day', which is why it prints the wrong value.

    Generally, when you’re confused about data like this, you can use the p method. puts will output things as they would be read by humans, but p prints them in a way that represents them as data. For instance, I figured this out by adding p json before the loop that printed them.

    So when do you use JSON? When you’re ready to serialize your data back to the file.


    Also note, that it would be better to do json = JSON.load(File.read fname) because you’ve opened the file, but never closed it (plus it’s cleaner to look at).

    Also, new_data = Array.new doesn’t actually do anything since you set it to a hash on the next line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem. I want to run a ruby script from another ruby
I am learning Ruby and have to do some homework, but have problem with
I am beginner in ruby + rails. I have problem to get values in
I am looking for the Ruby-est way to solve a problem. I have code
Hey dudes.i am having this problem while symlinking. I have successfully deployed a ruby
I have problem with UIWebView delay when the load image from url. In my
I've got a model creation form in rails which I also have returning JSON
I am trying to test the API from ankoder.com and have problem on the
I have some problem with replace string in Ruby. My Original string : What
I have some Ruby web apps that use OpenID for authentication and store the

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.