1

I have below database table having records, table rows can have various string records, pattern of records is like showed in table.

"domainVal", "idVal", "versionVal" - these are constants no dynamic but all other keys are dynamic, can have any string.

payment[] This is JsonArray in JSON

I want to generate dynamic JSON from these kind of records.

  1. I tried to create POJO but as records values are not constant it could be any string so can't create exact POJO,

  2. Tried to create Java collection to fit these records in java collection and can create json from Java collection but could not figure-out how to parse these records and can store in Java collection.

Table -

domain id version
identifier.domainKey identifier.idKey identifier.versionKey
payload.account.domain payload.account.id payload.order.version.version
payload.order.domain payload.order.id payload.order.version.dealVersion
payload.customer.customerType.domain payload.customer.customerType.id payload.customer.customerType.version
payload.payment[].deal.domain payload.payment[].deal.id payload.payment[].deal.version
payload.payment[].dealType.domain payload.payment[].dealType.id payload.payment[].dealType.version

Expected Json from above table -

{
"identifier": {
    "domainKey": "domainVal",
    "idKey": "idVal",
    "versionKey": "versionVal"
},
"payload": {
    "account": {
        "domain": "domainVal",
        "id": "idVal",
        "version": "versionVal"
    },
    "order": {
        "domainKey": "domainVal",
        "idKey": "idVal",
        "version": {
            "dealVersion": "versionVal"
        }
    },
    "customer": {
        "customerType": {
            "domain": "domainVal",
            "id": "idVal",
            "version": "versionVal"
        }
    },
    "payment": [
        {
            "deal": {
                "domain": "domainVal",
                "id": "idVal",
                "version": "versionVal"
            }
        },
        {
            "dealType": {
                "domain": "domainVal",
                "id": "idVal",
                "version": "versionVal"
            }
        }
    ]
}

}

Please someone share me a java code/Algorithm for this..

A simple recursion method would help me, Or Is there any standard library to do this

  • Are you saying your payment json is will have diffrent keys such that `deal`, `dealType`? and rest of the json are as is and will not change? – Rishal Aug 07 '22 at 12:38
  • @Rishal No keys are fixed even **payment** json key name may change to some **xyz** all names are dynamic – vishal Kshirsagar Aug 07 '22 at 14:11

0 Answers0