Home > Net >  Nest JSON output (list of children) within its parent (household) JSON using GSON
Nest JSON output (list of children) within its parent (household) JSON using GSON

Time:03-21

Goal is to avoid using JSONObject() and JSONArray() which requires manually putting each key/value pair, hence GSON or related solutions are appreciated.

I have two objects i.e. 1) Household, 2) Household Members with following properties as JSON (also same in model/pojo class)

Household:

{
    "household": {
        "hh_id": "1",
        "avg_expense": "2",
        "avg_income": "2",
        "beneficiary": "3278963",
        "beneficiary_id": "2342343242342",
        "beneficiary_name": "name"

    }
}

Household Members:

{
    "household_member": [{
        "age": "11",
        "creation_date": "2022-03-20 02:28:44",
        "disability": "1",
        "disability_oth": "",
        "disease": "1",
        "dob": "2011-03-20",
        "education": "1",
        "family_no": "1",
        "gender": "1",
        "hh_id": "1",
        "hh_mem_uuid": "7c828e43-b681-4e09-8cc4-279505244a24"
    }, {
        "age": "11",
        "creation_date": "2022-03-20 02:28:44",
        "disability": "1",
        "disability_oth": "",
        "disease": "1",
        "dob": "2011-03-20",
        "education": "1",
        "family_no": "1",
        "gender": "1",
        "hh_id": "1",
        "hh_mem_uuid": "7c828e43-b681-4e09-8cc4-279505244a24"
    }]
}

Generated using :

gson.toJson(household)
gson.toJson(household_members)

Is there any helper function in GSON library where I could possibly define children (household member) under its parent (household), similar to putting an a JSON array within an object like below:

jHHObject.put("household_member", jHouseholdMemberArray)

Which desire-ably results in below JSON:

{
    "household": {
        "hh_id": "1",
        "avg_expense": "2",
        "avg_income": "2",
        "beneficiary": "3278963",
        "beneficiary_id": "2342343242342",
        "beneficiary_name": "name",

        "household_member": [{
            "age": "11",
            "creation_date": "2022-03-20 02:28:44",
            "disability": "1",
            "disability_oth": "",
            "disease": "1",
            "dob": "2011-03-20",
            "education": "1",
            "family_no": "1",
            "gender": "1",
            "hh_id": "1",
            "hh_mem_uuid": "7c828e43-b681-4e09-8cc4-279505244a24"
        }, {
            "age": "11",
            "creation_date": "2022-03-20 02:28:44",
            "disability": "1",
            "disability_oth": "",
            "disease": "1",
            "dob": "2011-03-20",
            "education": "1",
            "family_no": "1",
            "gender": "1",
            "hh_id": "1",
            "hh_mem_uuid": "7c828e43-b681-4e09-8cc4-279505244a24"
        }]

    }
}

Model: Household:

import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey

@Entity(tableName = "household")
open class Household {

    @PrimaryKey(autoGenerate = true)
    var id: Int = 0
    var hh_uuid: String
    var creation_date: String
    var district: String
    var district_name: String
    var tehsil: String
    var tehsil_name: String
    var uc: String
    var uc_name: String
    var rv: String
    var rv_name: String
    var enumerator: String
    var enumerator_name: String
    var household: String
    var household_name: String
    var respondent_name: String
    var beneficiary: String
    var beneficiary_name: String
    var beneficiary_cnic: String
    var cell: String
    var young_couples: String
    var couples: String
    var total_hh_members: String
    var total_hh_male_18: String
    var total_hh_female_18: String
    var total_hh_male_17: String
    var total_hh_female_17: String
    var total_hh_male_5: String
    var total_hh_female_5: String
    var avg_income: String
    var avg_expense: String
    var earning_hand: String
    var family_prefer: String
    var taken_loan: String
    var taken_loan_amount: String
    var own_livestock: String
    var cow: String
    var sheep: String
    var camel: String
    var horse: String
    var comments: String
    var version_code: String
    var version_name: String
    var status_sent: String = "0"


    constructor(

        hh_uuid: String,
        creation_date: String,
        district: String,
        district_name: String,
        tehsil: String,
        tehsil_name: String,
        uc: String,
        uc_name: String,
        rv: String,
        rv_name: String,
        enumerator: String,
        enumerator_name: String,
        household: String,
        household_name: String,
        respondent_name: String,
        beneficiary: String,
        beneficiary_name: String,
        beneficiary_cnic: String,
        cell: String,
        young_couples: String,
        couples: String,
        total_hh_members: String,
        total_hh_male_18: String,
        total_hh_female_18: String,
        total_hh_male_17: String,
        total_hh_female_17: String,
        total_hh_male_5: String,
        total_hh_female_5: String,
        avg_income: String,
        avg_expense: String,
        earning_hand: String,
        family_prefer: String,
        taken_loan: String,
        taken_loan_amount: String,
        own_livestock: String,
        cow: String,
        sheep: String,
        camel: String,
        horse: String,
        comments: String,
        version_code: String,
        version_name: String,
        status_sent: String

    ) {
        this.hh_uuid = hh_uuid
        this.creation_date = creation_date
        this.district = district
        this.district_name = district_name
        this.tehsil = tehsil
        this.tehsil_name = tehsil_name
        this.uc = uc
        this.uc_name = uc_name
        this.rv = rv
        this.rv_name = rv_name
        this.enumerator = enumerator
        this.enumerator_name = enumerator_name
        this.household = household
        this.household_name = household_name
        this.respondent_name = respondent_name
        this.beneficiary = beneficiary
        this.beneficiary_name = beneficiary_name
        this.beneficiary_cnic = beneficiary_cnic
        this.cell = cell
        this.young_couples = young_couples
        this.couples = couples
        this.total_hh_members = total_hh_members
        this.total_hh_male_18 = total_hh_male_18
        this.total_hh_female_18 = total_hh_female_18
        this.total_hh_male_17 = total_hh_male_17
        this.total_hh_female_17 = total_hh_female_17
        this.total_hh_male_5 = total_hh_male_5
        this.total_hh_female_5 = total_hh_female_5
        this.avg_income = avg_income
        this.avg_expense = avg_expense
        this.earning_hand = earning_hand
        this.family_prefer = family_prefer
        this.taken_loan = taken_loan
        this.taken_loan_amount = taken_loan_amount
        this.own_livestock = own_livestock
        this.cow = cow
        this.sheep = sheep
        this.camel = camel
        this.horse = horse
        this.comments = comments
        this.version_code = version_code
        this.version_name = version_name
        this.status_sent = status_sent

    }

}

Model: Household Member

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "household_member")
class HouseholdMember {

    @PrimaryKey(autoGenerate = true)
    var id: Int = 0
    var hh_id: String
    var creation_date: String
    var household_id: String
    var hh_uuid: String
    var hh_mem_uuid: String
    var hh_member_bisp_id: String
    var hh_member_bisp_name: String
    var hh_name_ent: String
    var mem_name: String
    var dob: String
    var age: String
    var family_no: String
    var gender: String
    var marital_status: String
    var has_cnic: String
    var cnic: String
    var disability: String
    var disability_oth: String
    var disease: String
    var education: String
    var work: String
    var income: String
    var skill: String
    var skill_oth: String
    var beneficiary: String

    constructor(
        hh_id: String,
        creation_date: String,
        household_id: String,
        hh_uuid: String,
        hh_mem_uuid: String,
        hh_member_bisp_id: String,
        hh_member_bisp_name: String,
        hh_name_ent: String,
        mem_name: String,
        dob: String,
        age: String,
        family_no: String,
        gender: String,
        marital_status: String,
        has_cnic: String,
        cnic: String,
        disability: String,
        disability_oth: String,
        disease: String,
        education: String,
        work: String,
        income: String,
        skill: String,
        skill_oth: String,
        beneficiary: String

    ) {

        this.hh_id = hh_id
        this.creation_date = creation_date
        this.household_id = household_id
        this.hh_uuid = hh_uuid
        this.hh_mem_uuid = hh_mem_uuid
        this.hh_member_bisp_id = hh_member_bisp_id
        this.hh_member_bisp_name = hh_member_bisp_name
        this.hh_name_ent = hh_name_ent
        this.mem_name = mem_name
        this.dob = dob
        this.age = age
        this.family_no = family_no
        this.gender = gender
        this.marital_status = marital_status
        this.has_cnic = has_cnic
        this.cnic = cnic
        this.disability = disability
        this.disability_oth = disability_oth
        this.disease = disease
        this.education = education
        this.work = work
        this.income = income
        this.skill = skill
        this.skill_oth = skill_oth
        this.beneficiary = beneficiary


    }

}

CodePudding user response:

The easiest solution would be to add a household_members field to your household class; maybe this question can be helpful for this as well.

If that is not possible (or the overhead this creates for Room is not desired), you could use Gson to first serialize your household class to an in memory JSON document in the form of Gson's JsonObject, then serialize your household members to JSON as well and add them to the JsonObject. Afterwards you can convert the JsonObject to a JSON document string.

For example:

Gson gson = new Gson();
JsonObject householdJsonObject = gson.toJsonTree(household).getAsJsonObject();
JsonArray householdMembersJsonArray = gson.toJsonTree(householdMembers).getAsJsonArray();
householdJsonObject.add("household_members", householdMembersJsonArray);

// Convert the final JSON object to a JSON document string
// Any other toJson overload works as well, e.g. toJson(..., writer)
String json = gson.toJson(householdJsonObject);
  • Related