There are a lot of great Java libraries for working with JSON. One of these is Google’s Gson, which has the goal of providing simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa.

By default, the toJson() method will serialize all fields in the current class (and all super classes). Below is a simple example of creating a BdPerson object (class definition below), populating the fields with values, and outputting to JSON.

//converting object to JSON
public static void main(String[] args){
    Gson gson = new GsonBuilder().create();
    BdPerson p = new BdPerson();
    p.populateRandom(Fairy.create(), false);
    String s = gson.toJson(p);


	"id": 1,
	"username": "jacel",
	"email_address": "[email protected]",
	"phone_number": "181-798-081",
	"first_name": "Jace",
	"last_name": "Lewis",
	"middle_name": "",
	"sex": "MALE",
	"birthdate": "2013-11-13",
	"join_date": "2008-03-20T08:00:03.808-07:00",
	"previous_logins": 6191,
	"last_ip": ""

The BdPerson class:

package com.bigdatums.datagen;

import org.jfairy.Fairy;
import org.jfairy.producer.BaseProducer;
import org.jfairy.producer.DateProducer;
import org.jfairy.producer.person.Person;
import java.util.Random;

public class BdPerson {
    public transient String[] header = {"id","username","email_address","phone_number","first_name","last_name","middle_name","sex","birthdate","join_date","previous_logins","last_ip"};

    public int id;
    public String username;
    public String email_address;
    public String phone_number;
    public String first_name;
    public String last_name;
    public String middle_name;
    public String sex;
    public String birthdate;
    public String join_date;
    public int previous_logins;
    public String last_ip;

    private transient int currId = 0;
    private transient Random r = new Random();

    public void populateRandom(Fairy fairy, boolean randId) {
        Person person = fairy.person();
        DateProducer dp = fairy.dateProducer();
        BaseProducer bp = fairy.baseProducer();
        NetworkProducer np = fairy.networkProducer();

        if(randId) currId = r.nextInt(20000000);
        else currId++;

        id = currId;
        username = person.username();
        email_address =;
        phone_number = person.telephoneNumber();
        first_name = person.firstName();
        last_name = person.lastName();
        middle_name = person.middleName();
        sex =;
        birthdate = person.dateOfBirth().toLocalDate().toString();
        join_date = dp.randomDateBetweenYearAndNow(2005).toString();
        previous_logins = bp.randomBetween(1,10000);
        last_ip = np.ipAddress();

    public String[] toArray(){
        String[] personArr = {Integer.toString(id),username,email_address,phone_number,first_name,last_name,middle_name,sex,birthdate,join_date,Integer.toString(previous_logins),last_ip};
        return personArr;

    public String headerToString() {
        return headerToString("\t");

    public String headerToString(String sep) {
        return String.join(sep, header);

    public String toString() {
        return toString("\t");

    public String toString(String sep) {
        return String.join(sep, toArray());


You might have noticed that not all fields in BdPerson are included in the JSON output. There are a few ways to accomplish this which will be discussed in a later post. Documentation for Gson can be found here, and code examples can be found on the Big Datums GitHub repo.

Leave a Reply

Creating JSON with Gson