0

An app I am building receives some information in JSON, however the data is very poorly organized. I would like to rebuild the JSON output. The JSON I'm receiving is completely flat, and certain things should be nested. To illustrate what I mean:

I'm getting something like this:

{[
 {fullname: 'Joe', session: 'A', time: '5:00', room: 'Ballroom'},
 {fullname: 'Abe', session: 'B', time: '5:00', room: 'Bathroom'},
 {fullname: 'Mike', session: 'C', time: '6:00', room: 'Bathroom'},
]}

I want something like this:

   {
    rooms: [
      {
       name: 'Ballroom',
       sessions: [
        {
         title: 'A',
         speakers: [{name: 'Joe'}]
        }
       ]
      },
      {
       name: 'Bathroom',
       sessions: [
        {
         title: 'B',
         speakers: [{name: 'Abe'}]
        },
        {
         title: 'C',
         speakers : [{name: 'Mike'}]
        }
       ]
      }
    ]
   }

Are there any gems that are well equipped for doing something like this? Is there a specific part of the application this manipulation should be done in to follow MVC?

I should note that all this app does is receive this JSON and then makes API calls to another application to create/update information in that app's DB to reflect what's in the JSON.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
xxyyxx
  • 2,306
  • 3
  • 24
  • 34
  • 3
    once you decode the json, it's just a native data structure. treat it like you would any other structure: write some code to copy the contents into your new desired layout. – Marc B Jan 13 '15 at 22:02
  • Ruby's built in methods are pretty decent at performing this sort of data manipulation. For Rails, this kind of thing should be done in the a Model (not an ActiveRecord model, just a plain old Ruby object). – Ajedi32 Jan 13 '15 at 22:04
  • I'm not sure why you'd want to change to the second format. It's not as readable and will waste space on disk or on your network because of the added whitespace. While it's pretty, be kind to your devices and databases, and let JSON generate compact representations. If you need to see the data, then "prettify" just that record for display. Also, our eyes are better at picking out variations in text in columns than they are in long, strung-out text, so even for humans I'd stick with the original: Compare the first example with the second and look at the deltas. – the Tin Man Jan 13 '15 at 23:52

2 Answers2

1

Use:

JSON.pretty_generate your_hash

For example:

require 'json'
my_json = { :array => [1, 2, 3, { :sample => "hash"} ], :foo => "bar" }
puts JSON.pretty_generate(my_json)

refer to: How can I "pretty" format my JSON output in Ruby on Rails?

Community
  • 1
  • 1
Siwei
  • 19,858
  • 7
  • 75
  • 95
0

You can re-format using jbuilder or rabl gems. Usage and examples are pretty strait forward in their readme

Shlomi Zadok
  • 278
  • 4
  • 10
  • Looking at the example this is more than just formatting, it also includes data manipulation. – Nick Veys Jan 13 '15 at 22:05
  • 1
    Both those gems support that. You can actually write ruby code to manipulate the data. See the example at jbuilder: https://github.com/rails/jbuilder#jbuilder – Shlomi Zadok Jan 13 '15 at 22:07