My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
GenerationOfJson  
how to generate JSON from models
Updated Mar 7, 2012 by ipole...@gmail.com

Introduction

In context of a web application, especially when dealing with Ajax, it is handy to have your classes convert to to JSON to send to a browser. If you have a complex model with custom classes to be sent over, you will most likely write your JSON generation code, if however all you need is to convert ActiveJDBC models to JSON, this functionality is already available from your models without extra effort.

In more complicated situation, they probably would write some JSON generation code. However under simple condition, classes Model and LazyList already provide the basics.

Generate simple JSON from a model

Here is code that will provide stock JSON from a model:

Person p  = (Person)Person.findById(1);
String json = p.toJson(true);

The JSON produced will look something like this:

{
  "type":"activejdbc.test_models.Person",
  "id":"1",
  "updated_at":"2011-02-23 22:18:11.0",
  "graduation_date":"1954-12-01",
  "name":"John",
  "dob":"1934-12-01",
  "last_name":"Smith",
  "created_at":"2011-02-23 22:18:11.0"
}

this of course depends on the table structure. The boolean parameter to the method toJson() is whether to generate human readable format or a single string.

Specify output attributes for generated JSON

A variation on the example above is to provide a list of attributes that you are interested in, so that only these attributes are included:

Person p  = (Person)Person.findById(1);
String json = p.toJson(true, "last_name", "dob");

The resulting JSON will only include the attributes specified:

{
  "dob":"1934-12-01",
  "last_name":"Smith"
}

Inclusion of dependencies

When a model is has relationships, the generated JSON will loop through them to include their JSON into the parent JSON as well:

List<User> personList = User.findAll().orderBy("id").include(Address.class);
User u = personList.get(0);
String json = u.toJson(true);

result:

{
  "type":"activejdbc.test_models.User",
  "id":"1",
  "first_name":"Marilyn",
  "email":"mmonroe@yahoo.com",
  "last_name":"Monroe",
  "children" : {
    addresses : [
    {
      "type":"activejdbc.test_models.Address",
      "id":"1",
      "zip":"60606",
      "state":"IL",
      "address1":"123 Pine St.",
      "address2":"apt 31",
      "user_id":"1",
      "city":"Springfield"
    },
    {
      "type":"activejdbc.test_models.Address",
      "id":"2",
      "zip":"60606",
      "state":"IL",
      "address1":"456 Brook St.",
      "address2":"apt 21",
      "user_id":"1",
      "city":"Springfield"
    },
    {
      "type":"activejdbc.test_models.Address",
      "id":"3",
      "zip":"60606",
      "state":"IL",
      "address1":"23 Grove St.",
      "address2":"apt 32",
      "user_id":"1",
      "city":"Springfield"
    }
    ]
  }
}

Generate JSON from a resultset

Generating JSON from a LazyList is equally easy:

LazyList<User> personList = User.findAll().orderBy("id").include(Address.class);
String json = personList.toJson(true);

An example of generated JSON:

[
  {
    "type":"activejdbc.test_models.User",
    "id":"1",
    "first_name":"Marilyn",
    "email":"mmonroe@yahoo.com",
    "last_name":"Monroe",
    "children" : {
      addresses : [
        {
        "type":"activejdbc.test_models.Address",
        "id":"1",
        "zip":"60606",
        "state":"IL",
        "address1":"123 Pine St.",
        "address2":"apt 31",
        "user_id":"1",
        "city":"Springfield"
      },
        {
        "type":"activejdbc.test_models.Address",
        "id":"2",
        "zip":"60606",
        "state":"IL",
        "address1":"456 Brook St.",
        "address2":"apt 21",
        "user_id":"1",
        "city":"Springfield"
      },
        {
        "type":"activejdbc.test_models.Address",
        "id":"3",
        "zip":"60606",
        "state":"IL",
        "address1":"23 Grove St.",
        "address2":"apt 32",
        "user_id":"1",
        "city":"Springfield"
      }
    ]
    }
  },
  {
    "type":"activejdbc.test_models.User",
    "id":"2",
    "first_name":"John",
    "email":"jdoe@gmail.com",
    "last_name":"Doe",
    "children" : {
      addresses : [
        {
        "type":"activejdbc.test_models.Address",
        "id":"4",
        "zip":"60606",
        "state":"IL",
        "address1":"143 Madison St.",
        "address2":"apt 34",
        "user_id":"2",
        "city":"Springfield"
      },
        {
        "type":"activejdbc.test_models.Address",
        "id":"5",
        "zip":"60606",
        "state":"IL",
        "address1":"153 Creek St.",
        "address2":"apt 35",
        "user_id":"2",
        "city":"Springfield"
      },
        {
        "type":"activejdbc.test_models.Address",
        "id":"6",
        "zip":"60606",
        "state":"IL",
        "address1":"163 Gorge St.",
        "address2":"apt 36",
        "user_id":"2",
        "city":"Springfield"
      },
        {
        "type":"activejdbc.test_models.Address",
        "id":"7",
        "zip":"60606",
        "state":"IL",
        "address1":"173 Far Side.",
        "address2":"apt 37",
        "user_id":"2",
        "city":"Springfield"
      }
    ]
    }
  }
]

as you can see, the two users were generated. Since the include() was used, the corresponding children from the ADDRESSES table were queried too for their JSON.

Back to Features

Comment by antonin....@gmail.com, Oct 20, 2011

http://www.json.org/fatfree.html

numbers and booleans must be non quoted ...

Comment by mariosta...@gmail.com, Feb 7, 2012

Any possibility of disabling generation of type of Model Class in the JSON String?

Comment by localmar...@gmail.com, Mar 5, 2012

When I specify the list of attributes I want, I still get the model_class attribute returned.

Comment by project member ipole...@gmail.com, Mar 5, 2012

yes, this is correct, if you feel this is a defect, please file it: http://code.google.com/p/activejdbc/issues/list


Sign in to add a comment
Powered by Google Project Hosting