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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:05:14+00:00 2026-05-24T13:05:14+00:00

I want to break down a JSON string into smaller objects. I have two

  • 0

I want to break down a JSON string into smaller objects. I have two servers, one acting as the web-app interface to the whole application and the other is a repository/database.

I’m able to retrieve information from the repository to the web-app as JSON, but after that I don’t know how to return it.

Here’s a sample of the JSON being returned:

{"respPages":[{"page":{"page_url":"http://www.google.com/","created_at":"2011-08-10T11:00:19Z","website_id":1,"updated_at":"2011-08-10T11:00:19Z","id":1}},{"page":{"page_url":"http://www.blank.com/services/content_services/","created_at":"2011-08-10T11:02:46Z","website_id":1,"updated_at":"2011-08-10T11:02:46Z","id":2}}],"respSite":{"website":{"created_at":"2011-08-10T11:00:19Z","website_id":null,"updated_at":"2011-08-10T11:00:19Z","website_url":null,"id":1}},"respElementTypes":[{"element_type":{"created_at":"2011-08-10T11:00:19Z","updated_at":"2011-08-10T11:00:19Z","id":1,"tag_name":"head"}},

There are four tags in the JSON:

page
website
elementType
elementData

I would like to create four arrays and populate them with the object that matches these tags.

I would image the code is something like this:

#Get the json from repo using net/http
 uri = URI.parse("http://127.0.0.1:3007/repository/infoid/1.json")
 http = Net::HTTP.new(uri.host, uri.port)
 response = http.request(Net::HTTP::Get.new(uri.request_uri))


@x = response.to_hash

@pages = Array.new
@websites= Array.new
@elementDatas = Array.new
@elementTypes = Array.new

#enter code here`#For every bit of the hash, find out what it is and allocate it accordingly
    @x.each_with_index do |e,index|
    if e.tagName == pages #Getting real javascripty here. There must be someway to check the tag or title of the element
    @pages[index]=e
    end

My goal for the returned value is to have four arrays, each containing different types of objects:

  @pagesArray[1]

should contain the first occurrence of a page object in the JSON string. Then do the same for the other ones.

Of course I’d need to break down the object further but once I can break down the top level and categorize them, then I can go deeper.

In the JSON there are already tag titles respPages and respWebsites which group all the objects.

How do I turn JSON back into objects in Ruby and reference them using something like the tag name?

  • 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-24T13:05:15+00:00Added an answer on May 24, 2026 at 1:05 pm

    You should be able to decode anything in JSON format using the standard JSON library:

    JSON.load(...)
    

    It will throw exceptions on malformed JSON data, so be sure to test it thoroughly and make sure it can handle all the important cases.

    If you’re trying to navigate the structure of the JSON itself, you probably need to write a series of recursive methods that handle each case along the way. A good pattern to start with is this:

    @data.each do |key, value|
      case (key)
      when 'someKey'
        handle_some_key(value)
      when 'otherKey'
        handle_other_key(value)
      end
    end
    

    You can either break out the behavior into methods as in this example, or inline it if the logic is fairly straightforward.

    As a note, an alternative to Array.new is simply [ ] as it is in JavaScript. For example:

    @pages = [ ]
    

    You’ll see this used frequently in most Ruby examples. The alternative to Hash.new is { }.

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

Sidebar

Related Questions

I want to create a client side mail creator web page. I know the
In C# i have an array of Calendar objects each Calendar object has an
I have a set of students at a high school. The counselors want to
Trying to work out distance between two points (lat & lng) I have an
When creating reports using BIRT 2.3.1, I don't want page breaks inside tables or
Want to know what the stackoverflow community feels about the various free and non-free
Want my FireFox at work to be in sync with my FireFox at my
I want to loop over the contents of a text file and do a
I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal
I want to create a Java application bundle for Mac without using Mac. According

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.