Rails rabl json format

Posted by brabertaser1992 on Stack Overflow See other posts from Stack Overflow or by brabertaser1992
Published on 2013-04-06T17:30:31Z Indexed on 2013/06/29 4:21 UTC
Read the original article Hit count: 175

Filed under:
|
|

I'm displaying data from my db in json format using rabl to parse it for android... My json looks like so:

[
   {
      "bank":{
         "central_office_address":"ololo",
         "license":"12312312",
         "location_id":3,
         "name":"Pbank",
         "tax_number":"12312312",
         "year_of_foundation":1987
      }
   },
   {
      "bank":{
         "central_office_address":"sdfsdf sdf",
         "license":"321312",
         "location_id":3,
         "name":"Bbank",
         "tax_number":"321321",
         "year_of_foundation":1999
      }
   }
]

I need my json in a format like:

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "[email protected]",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c201",
                "name": "Johnny Depp",
                "email": "[email protected]",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        }
    ]
}

Such data is normally parsed in java....

My rabl view:

object @banks

attributes :central_office_address, :license, :location_id, :name, :tax_number, :year_of_foundation

How do I change its output to match the second example?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about JSON