Hi
i have below Json Schema. How to create classes for below schema
{
"$schema": "json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"supplyType": {
"type": "string",
"maxLength": 1,
"minLength": 1,
"enum": [ "O","I" ],
"description": "Supply Type"
},
"subSupplyType": {
"type": "string",
"description": "Sub Supply Type"
},
"subSupplyDesc": {
"type": "string",
"maxLength": 20,
"description": "Other Sub Supply Description"
},
"docType": {
"type": "string",
"enum": [ "INV", "CHL", "BIL","BOE","OTH" ],
"description": "Document Type"
},
"docNo": {
"type": "string",
"maxLength": 16,
"description": "Document Number (Alphanumeric with / and - are allowed)"
},
"docDate": {
"type": "string",
"pattern": "[0-3][0-9]/[0-1][0-9]/[2][0][1-2][0-9]",
"description": "Document Date"
},
"fromGstin": {
"type": "string",
"maxLength": 15,
"minLength": 15,
"pattern": "[0-9]{2}[0-9|A-Z]{13}",
"description": "From GSTIN (Supplier or Consignor)"
},
"fromTrdName": {
"type": "string",
"maxLength": 100,
"description": "From Trade Name (Consignor Trade name)"
},
"fromAddr1": {
"type": "string",
"maxLength": 120,
"description": "From Address Line 1 (Valid Special Chars #,-,/)"
},
"fromAddr2": {
"type": "string",
"maxLength": 120,
"description": "From Address Line 2(Valid Special Chars # , - ,/)"
},
"fromPlace": {
"type": "string",
"maxLength": 50,
"description": "From Place"
},
"actFromStateCode": {
"type": "integer",
"maximum": 99,
"description": "Actual From State Code"
},
"fromPincode": {
"type": "integer",
"maximum": 999999,
"minimum": 100000,
"description": "From Pincode"
},
"fromStateCode": {
"type": "integer",
"maximum": 99,
"description": "From State Code"
},
"toGstin": {
"type": "string",
"maxLength": 15,
"minLength": 15,
"pattern": "[0-9]{2}[0-9|A-Z]{13}",
"description": "To GSTIN (Consignee or Recipient)"
},
"toTrdName": {
"type": "string",
"maxLength": 100,
"description": "To Trade Name (Consignee Trade name or Recipient Trade name)"
},
"toAddr1": {
"type": "string",
"maxLength": 120,
"description": "To Address Line 1 (Valid Special Chars #,-,/)"
},
"toAddr2": {
"type": "string",
"maxLength": 120,
"description": "To Address Line 2 (Valid Special Chars #,-,/)"
},
"toPlace": {
"type": "string",
"maxLength": 50,
"description": "To Place"
},
"toPincode": {
"type": "integer",
"description": "To Pincode"
},
"actToStateCode": {
"type": "integer",
"maximum": 99,
"description": "Actual To State Code"
},
"toStateCode": {
"type": "integer",
"maximum": 99,
"description": "To State Code"
},
"transactionType": {
"type": "integer",
"maximum": 4,
"description": "Transaction type"
},
"totalValue": {
"type": "number",
"multipleOf": 0.01,
"description": "Sum of Taxable value"
},
"cgstValue": {
"type": "number",
"multipleOf": 0.01,
"description": "CGST value"
},
"sgstValue": {
"type": "number",
"multipleOf": 0.01,
"description": "SGST value"
},
"igstValue": {
"type": "number",
"multipleOf": 0.01,
"description": "IGST value"
},
"cessValue": {
"type": "number",
"multipleOf": 0.01,
"description": "Cess value"
},
"cessNonAdvolValue": {
"type": "number",
"multipleOf": 0.01,
"description": "Cess Non Advol value"
},
"otherValue": {
"type": "number",
"multipleOf": 0.01,
"description": "Other charges, if any "
},
"totInvValue": {
"type": "number",
"multipleOf": 0.01,
"description": "Total Invoice Value (Including taxable value, tax value,and other charges if any)"
},
"transMode": {
"type": "string",
"enum": ["1","2","3","4"],
"description": "Mode of transport (Road-1, Rail-2, Air-3, Ship-4) "
},
"transDistance": {
"type": "string",
"description": "Distance (<4000 km) "
},
"transporterName": {
"type": "string",
"maxLength": 100,
"description": "Name of the transporter"
},
"transporterId": {
"type": "string",
"pattern": "[0-9]{2}[0-9|A-Z]{13}",
"description": "15 Digit Transporter GSTIN/TRANSIN"
},
"transDocNo": {
"type": "string",
"maxLength": 15,
"description": "Transport Document Number (Alphanumeric with / and – are allowed)"
},
"transDocDate": {
"type": "string",
"description": "Transport Document Date"
},
"vehicleNo": {
"type": "string",
"minLength": 7,
"maxLength": 15,
"description": "Vehicle Number"
},
"vehicleType": {
"type": "string",
"description": "Vehicle Type"
},
"itemList": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"productName": {
"type": "string",
"maxLength": 100,
"description": "Product / Item Name"
},
"productDesc": {
"type": "string",
"maxLength": 100,
"description": "Product / Item description"
},
"hsnCode": {
"type": "number",
"description": "HSN Code"
},
"quantity": {
"type": "number",
"description":"Quantity"
},
"qtyUnit": {
"type": "string",
"maxLength": 3,
"minLength": 3,
"description": "Unit"
},
"taxableAmount": {
"type": "number",
"multipleOf": 0.01,
"description":"Taxable Amount"
},
"sgstRate": {
"type": "number",
"multipleOf": 0.001,
"description":"SGST Rate of Tax"
},
"cgstRate": {
"type": "number",
"multipleOf": 0.001,
"description":"CGST Rate of Tax"
},
"igstRate": {
"type": "number",
"multipleOf": 0.001,
"description":"IGST Rate of Tax"
},
"cessRate": {
"type": "number",
"multipleOf": 0.001,
"description":"Cess Rate of Tax"
},
"cessNonadvol": {
"type": "number",
"description":"Cess Non-Advolerum"
}
},
"required": [
"hsnCode",
"taxableAmount"
]
}
]
}
},
"required": [
"supplyType",
"subSupplyType",
"docType",
"docNo",
"docDate",
"fromGstin",
"fromPincode",
"fromStateCode",
"toGstin",
"toPincode",
"toStateCode",
"transDistance",
"itemList",
"actToStateCode",
"actFromStateCode",
"totInvValue",
"transactionType"
] }
Thanks
Sophia CarterPosted May 15, 2025, 1:47 AM
To create classes based on the provided JSON schema, you can follow an object-oriented approach that mirrors the schema structure. Each class will represent a specific section of the schema, and the properties within the class will correspond to the JSON schema properties. Here's a high-level overview to guide you in creating classes that align with the given JSON schema:
1. You can start by defining a class for the main object, let's call it `InvoiceDetails`:
2. Create separate classes for nested objects like `ItemList`, which contains an array of items:
3. Define a class for the `Item` object within the `ItemList`:
4. Continue to create classes for each section of the schema, ensuring that the properties and validations match the JSON schema definitions.
Remember to include appropriate validation logic within the class constructors or setter methods to ensure data integrity.
Additionally, you can consider using popular Java libraries like Jackson or Gson to map JSON data to Java objects effortlessly. These libraries can significantly simplify the conversion process and handle JSON schema validation automatically.
By following this approach and mapping each class to the corresponding JSON schema properties, you can effectively create a Java representation of the schema that aligns with the data structure. Let me know if you need further clarification or specific examples for any part of the schema!