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 8826163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:04:11+00:00 2026-06-14T07:04:11+00:00

I have a single-line JSON file I’m trying to iterate over and looking for

  • 0

I have a single-line JSON file I’m trying to iterate over and looking for the best way to loop through it.

Exploded out, it looks something like:

{
 "system": "Test",
 "Continents": [
    {
        "Continent": {
            "name": "Europe",
            "location": "North"
         },
        "Continent": {
            "name": "Australia",
            "location": "South"
         },
        "Continent": {
            "name": "Asia",
            "location": "North"
         }
     }  
  ]
}

To start, I’m loading in the JSON with the following, which successfully loads the full JSON.

File.open(json_file, 'r') do |file|

    file.each do |line|            

      JSON.parse(line).each do |item|

        ## CODE TO ITERATE HERE

      end        
    end
 end

What I’m not sure how to do is to loop through the Continents section and pull the associated records. The goal would be to loop through and output the info in a list/table/display.

Thanks for the help – let me know if I can clarify at all.

  • 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-14T07:04:13+00:00Added an answer on June 14, 2026 at 7:04 am

    JSON.parse will convert your JSON string to a Ruby hash, so you can work with it as you would any other:

    json = JSON.parse(line)
    json["Continents"].each do |continent|
      # do something
    end
    

    There is a separate problem with your data, however. If you actually use JSON.parse with the data you posted, you should wind up with a result like this:

    {"system"=>"Test", "Continents"=>[{"Continent"=>{"name"=>"Asia", "location"=>"North"}}]}
    

    You’ll notice there is only one continent – that’s because Ruby hashes only support one value per key, so the Continent key is being overwritten for each Continent in the JSON. You might need to look at a different way of formatting that data.

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

Sidebar

Related Questions

I have a long single-line CSS file that I would like to convert using
I have a file (10-20MB) containing data, where each line is a single piece
I have an html page with a single-line javascript comment as follows: // {key1:value1},
I have a user control which has a DateTimePicker overlaid with the single line
I need to have various text view on a single line. And I want
I'm have a server application, and there is a single line Certificate[] certs =
I have a service request form. A series of textboxes (single and multi line),
I have a textfile and on each line is a single word followed by
Ext-js Chart, I have a line graph, and I wish to add a single
I have a (single line) TextCtrl. The user types data into this. When they

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.