1

Below is a sample of final json I pass to javascript. I will use the (yajl) ruby library to create this json from a hash.

The question is what should a ruby hash that produces the json below look like?

var data = [{
       data: "basics",
       attr: {},
        children: [
         {data: "login", attr: {run: "run"},
           children: [                   
           {data: "login", attr: {}}
          ]
         } ,
         {data: "Academic Year", attr: {run: "run"},
          children: [                   
           {data: "login", attr: {}},
           {data: "Academic Year", attr: {filter: "mini", SOF: "yes"}}
          ]

         }
        ]
      }];
marko
  • 9,029
  • 4
  • 30
  • 46
Radek
  • 13,813
  • 52
  • 161
  • 255

4 Answers4

4
  1. Copy your code (exactly) after the var and before the ;
  2. Paste into Ruby (1.9+)
    At this point you are done. To prove it...
  3. require "json"
  4. puts data.to_json

Result (with extra linebreaks):

#=> [{"data":"basics","attr":{}, "children":[
#=>    {"data":"login","attr":{"run":"run"},"children":[
#=>      {"data":"login","attr":{}}
#=>    ]},
#=>    {"data":"Academic Year","attr":{"run":"run"},"children":[
#=>      {"data":"login","attr":{}},
#=>      {"data":"Academic Year","attr":{"filter":"mini","SOF":"yes"}}
#=>    ]}
#=> ]}]
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • +1 That's pretty much what I do any time I need the JSON, or YAML, version of a data structure rather than trying to generate it by hand. – the Tin Man Apr 28 '11 at 03:32
  • when I copy the exact code I get ` odd number list for Hash` . Using ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32] – Radek May 04 '11 at 01:09
  • 1
    @Radek Under Ruby 1.8.7 you you will need to modify the Ruby source code so that all instances of `foo: ...` instead show `:foo => ...`. The Hash literal notation of `foo: bar` is a shorthand only available in Ruby 1.9+ – Phrogz May 04 '11 at 01:18
  • Also, Radek, that is not an error you would get if you copy/pasted exactly your code into 1.8.7, which means you're doing something other than what I suggested. You should probably create a new question or [pastie](http://pastie.org) of your code. – Phrogz May 04 '11 at 01:26
3

You can find out what sort of data structure would produce that JSON yourself easily enough:

require 'active_support'
json = '[{
       data: "basics",
       attr: {},
        children: [
         {data: "login", attr: {run: "run"},
           children: [
           {data: "login", attr: {}}
          ]
         } ,
         {data: "Academic Year", attr: {run: "run"},
          children: [
           {data: "login", attr: {}},
           {data: "Academic Year", attr: {filter: "mini", SOF: "yes"}}
          ]

         }
        ]
      }]'
puts ActiveSupport::JSON.decode(json).inspect

And then a quick reformatting of the output gives you this:

[
    {
        "data" => "basics",
        "children" => [
            {
                "data" => "login", 
                "children" => [
                    {
                        "data" => "login", 
                        "attr" => { }
                    }
                ], 
                "attr" => {
                    "run" => "run"
                }
            }, 
            {
                "data" => "Academic Year", 
                "children" => [
                    {
                        "data" => "login", 
                        "attr" => { }
                    }, 
                    {
                        "data" => "Academic Year", 
                        "attr" => {
                            "filter" => "mini", 
                            "SOF" => "yes"
                        }
                    }
                ], 
                "attr" => {
                    "run" => "run"
                }
            }
        ], 
        "attr" => { }  
    } 
]

There are probably easier ways but the above will do for a quick one-shot hack.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • could you elaborate on "easier ways" :-) I will create the hash on the fly based on file directory structure and content of these files. I didn't know how to structure this hash .. but if there is anything easier... :-) – Radek Apr 28 '11 at 03:04
  • I find it much easier to use the regular JSON module, instead of loading in Active_Support. – the Tin Man Apr 28 '11 at 03:33
  • @The Tin Man: I just used the Rails one because I've been doing a lot of Rails and JSON lately. @Radek: I did say "probably" :) Looks like Phrogz has something that produces more readable output without the hand formatting. – mu is too short Apr 28 '11 at 03:36
1

Your question is not too clear. Do you mean what is the Ruby structure that would create the JSON you show in your question?

If so, then here you go.... Note that the base-level structure is a Ruby Array because your JSON base level structure is also an Array.

mydata = [{
           'data' => "basics",
           'attr' => {},
           'children' => [{
                          'data' => "login",
                          'attr' => {'run' => "run"},
                          'children' => [{
                                         'data' => "login",
                                         'attr' => {}
                                        }]
                          },
                          {
                           'data' => "Academic Year",
                           'attr' => {'run' => "run"},
                           'children' => [{
                                          'data' => "login",
                                          'attr' => {}
                                          },
                                          {
                                           'data' => "Academic Year",
                                           'attr' => {'filter' => "mini", 
                                                      'SOF' => "yes"}
                                          }]
                           }]
         }]
Phrogz
  • 296,393
  • 112
  • 651
  • 745
Larry K
  • 47,808
  • 15
  • 87
  • 140
-1

I just ran that in my browser and it appears to be valid JSON. Is that your question?

eggie5
  • 1,920
  • 18
  • 25
  • my question is: how a hash that can be converted into above json should look like – Radek Apr 28 '11 at 02:30
  • 1
    Downvoted not only because this missed the question, but because while that is a valid _JS literal_, it is not valid _JSON_. – Phrogz Apr 28 '11 at 03:20
  • @Phrogz: could you explain this to me? `is a valid JS literal, it is not valid JSON` – Radek Apr 28 '11 at 04:33
  • 1
    @Radek An object literal with a regular expression literal—`{ foo:/bar/ }`—is valid JS but is not valid [JSON](http://json.org): JSON does not support regular expressions. Similarly, a lot of valid JS code is not valid JSON. In this case, JS allows valid identifiers to be used directly as attributes of object literals—`{ foo:42 }`—but JSON requires that they be be double-quoted, i.e. `{ "foo":42 }`. – Phrogz Apr 28 '11 at 12:55