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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:10:00+00:00 2026-06-08T12:10:00+00:00

I have some JSON that I pull in and read into an object @items

  • 0

I have some JSON that I pull in and read into an object @items:

[
 {
  {
    "id": "A",
    "description": "a_description_one"
    "value": some_alphanumeric_string
  }, 
  {
    "id": "B",
    "description": "b_description_one"
    "value": some_alphanumeric_string
  }, 
  {
    "id": "C",
    "description": "c_description_one"
    "value": some_alphanumeric_string
  }
 }, 
 {
  {
    "id": "C",
    "description": "c_description_3"
    "value": some_alphanumeric_string
  }, 
  {
    "id": "A",
    "description": "a_description_3"
    "value": some_alphanumeric_string
  }, 
  {
    "id": "B",
    "description": "b_description_3"
    "value": some_alphanumeric_string
  }
 }, 
 ...
] 

My goal is to output two tables, as follows, in HTML:

 -----------------------------------------------------
|        A        |        B        |        C        |
|-----------------------------------------------------|
|a_description_one|b_description_one|c_description_one|
|-----------------------------------------------------|
|a_description_two|b_description_two|c_description_two|
|-----------------------------------------------------|
|a_description_3  |b_description_3  |c_description_3  |
 -----------------------------------------------------

And a second table:

 ----------------------------------------------------
|  C value  | C description  |  Count       | Change |
|----------------------------------------------------|
|some string|it's description|times appeared| change |
 ----------------------------------------------------

While the first table is fairly straightforward, I wasn’t able to find a good way to code it because I didn’t know the order of the descriptors. As such, I have:

<table> 
  <thead> 
    <tr>
      <th>a</th>
      ...
    </tr>
  </thead>
  <tbody> 
    <% for item in @items %>
      <% for portion in item %> 
        <% if portion[0]["id"] == "A" %> 
           <% a = portion[0] %>
        <% end %>
      ...
      <% end %> 
      <tr> 
        <td><%= a["description"].to_s %></td>
        ...
      </tr> 
    <% end %>
  </tbody>
</table> 

Which seems awful.

As for the second table, all I’m trying to do is look at all the C values, and if there are multiples with the same C value concatenate them. I also want to load yesterdays data, which I can get from the server and have as @items_old, and compare the counts of items with the same values to yesterdays ones. I have no idea where to go with this.

  • 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-08T12:10:01+00:00Added an answer on June 8, 2026 at 12:10 pm

    It sounds like the description arrays are out of order, but you need them in order. So just sort the arrays before you iterate through them.

    <% for item in @items %>
      <% for portion in item.sort_by { |obj| obj['id'] } %> 
        <tr>
          <td><%= obj["description"].to_s %></td>
          ...
        </tr>
      <% end %>
    <% end %>
    

    Alternatively, ruby arrays do have a Array#find method that takes a block and returns an item when the block returns true. So if you want to be real explicit about it you can do:

    <% for item in @items %>
      <% for portion in item %> 
        <tr>
          <td><%= item.find { |obj| obj['id'] == 'A' }["description"].to_s %></td>
          <td><%= item.find { |obj| obj['id'] == 'B' }["description"].to_s %></td>
          <td><%= item.find { |obj| obj['id'] == 'C' }["description"].to_s %></td>
        </tr>
      <% end %>
    <% end %>
    

    But that starts to get pretty hairy for a view. I would strongly suggest refactoring that logic to some helper methods. The downside here is that you ever have a D in in there you suddenly have a lot of code to change. Simply iterating through sorted arrays you dont have to really care how many are in there, which can be good or bad depending on what you are doing.


    Lastly you could transform this data in ruby into a format that is more how you want. Build new arrays and hashes from this data and presented the processed @items object to the view. Or even better wrap all that in a class that would have methods to parse this data and then provide a simple interface to its guts.

    If this data is reasonably complex, and used in a lot of different ways, this is likely the most “correct” approach as it encapsulates the data logic far away from the presentation logic.

    class Descriptions
      def initialize(json)
        @data = JSON.parse(json)
      end
    
      def get_description(index, id)
        description_obj = @data[index].find do |obj|
          obj['id'] == id
        end
    
        description_obj['description'].to_s
      end
    end
    
    my_model = Descriptions.new(json)
    my_model.get_description(0, 'A')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some code that I don't have control over is overriding the global JSON object
I have some json object that I decoded, and one of the attributes starts
Inside my .aspx I have some JSON code that looks like this: function someFunctionName()
I have some data that I have to serialize to JSON. I'm using JSON.NET.
I have a spring action that I am rendering some json from the controller,
I have an jQuery JSON request, that loads some JSON from another server (ex.
I have a function that returns json (networks_function.php builds up some arrays and encodes
I have an iPhone app that uses the json-framework . I moved some of
I have some JSON data that looks like this: { 910719: { id: 910719,
Greetings all, I have some JSON code that looks like this: { playlist: [

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.