+ Start a Discussion
Savvy AdminSavvy Admin 

How to parse nested JSON string from finance gateway to Apex object?

Hi,

I have the following JSON retrieved from a web service request. 
[
	{
		"WarrantyPremium_type_info": [
			"WarrantyPremium",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"WarrantyPremium": null,
		"VehicleDescription_type_info": [
			"VehicleDescription",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"VehicleDescription": null,
		"TruckGapPremium_type_info": [
			"TruckGapPremium",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"TruckGapPremium": null,
		"TotalAssistPremium_type_info": [
			"TotalAssistPremium",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"TotalAssistPremium": {
			"StampDuty_type_info": [
				"StampDuty",
				"http://services.fastr.com.au/Quotation/Data",
				null,
				"1",
				"1",
				"false"
			],
			"StampDuty": 112.27,
			"Premium_type_info": [
				"Premium",
				"http://services.fastr.com.au/Quotation/Data",
				null,
				"1",
				"1",
				"false"
			],
			"Premium": 1235,
			"Gst_type_info": [
				"Gst",
				"http://services.fastr.com.au/Quotation/Data",
				null,
				"1",
				"1",
				"false"
			],
			"Gst": 102.07,
			"field_order_type_info": [
				"AvailablePaymentMethods",
				"CostExGst",
				"CoverTypeCode",
				"EstablishmentFee",
				"Gst",
				"Premium",
				"StampDuty"
			],
			"EstablishmentFee_type_info": [
				"EstablishmentFee",
				"http://services.fastr.com.au/Quotation/Data",
				null,
				"1",
				"1",
				"false"
			],
			"EstablishmentFee": 44,
			"CoverTypeCode_type_info": [
				"CoverTypeCode",
				"http://services.fastr.com.au/Quotation/Data",
				null,
				"0",
				"1",
				"true"
			],
			"CoverTypeCode": "TC1",
			"CostExGst_type_info": [
				"CostExGst",
				"http://services.fastr.com.au/Quotation/Data",
				null,
				"1",
				"1",
				"true"
			],
			"CostExGst": 500,
			"AvailablePaymentMethods_type_info": [
				"AvailablePaymentMethods",
				"http://services.fastr.com.au/Quotation/Data",
				null,
				"1",
				"1",
				"true"
			],
			"AvailablePaymentMethods": {
				"PaymentMethod_type_info": [
					"PaymentMethod",
					"http://services.fastr.com.au/Quotation/Data",
					null,
					"0",
					"-1",
					"true"
				],
				"PaymentMethod": [
					{
						"Payable_type_info": [
							"Payable",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"false"
						],
						"Payable": 1279,
						"NumberOfInstalments_type_info": [
							"NumberOfInstalments",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"0",
							"1",
							"true"
						],
						"NumberOfInstalments": null,
						"Name_type_info": [
							"Name",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"true"
						],
						"Name": "Full - Credit Card",
						"InstalmentAmount_type_info": [
							"InstalmentAmount",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"0",
							"1",
							"true"
						],
						"InstalmentAmount": null,
						"field_order_type_info": [
							"Code",
							"InstalmentAmount",
							"Name",
							"NumberOfInstalments",
							"Payable"
						],
						"Code_type_info": [
							"Code",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"true"
						],
						"Code": "CREDITCARD",
						"apex_schema_type_info": [
							"http://services.fastr.com.au/Quotation/Data",
							"true",
							"false"
						]
					},
					{
						"Payable_type_info": [
							"Payable",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"false"
						],
						"Payable": 1279,
						"NumberOfInstalments_type_info": [
							"NumberOfInstalments",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"0",
							"1",
							"true"
						],
						"NumberOfInstalments": null,
						"Name_type_info": [
							"Name",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"true"
						],
						"Name": "Full - Dealer Cheque",
						"InstalmentAmount_type_info": [
							"InstalmentAmount",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"0",
							"1",
							"true"
						],
						"InstalmentAmount": null,
						"field_order_type_info": [
							"Code",
							"InstalmentAmount",
							"Name",
							"NumberOfInstalments",
							"Payable"
						],
						"Code_type_info": [
							"Code",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"true"
						],
						"Code": "DEALERCHEQUE",
						"apex_schema_type_info": [
							"http://services.fastr.com.au/Quotation/Data",
							"true",
							"false"
						]
					},
					{
						"Payable_type_info": [
							"Payable",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"false"
						],
						"Payable": 1279,
						"NumberOfInstalments_type_info": [
							"NumberOfInstalments",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"0",
							"1",
							"true"
						],
						"NumberOfInstalments": null,
						"Name_type_info": [
							"Name",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"true"
						],
						"Name": "Full - Finance Company",
						"InstalmentAmount_type_info": [
							"InstalmentAmount",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"0",
							"1",
							"true"
						],
						"InstalmentAmount": null,
						"field_order_type_info": [
							"Code",
							"InstalmentAmount",
							"Name",
							"NumberOfInstalments",
							"Payable"
						],
						"Code_type_info": [
							"Code",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"true"
						],
						"Code": "FINANCECOMPANY",
						"apex_schema_type_info": [
							"http://services.fastr.com.au/Quotation/Data",
							"true",
							"false"
						]
					},
					{
						"Payable_type_info": [
							"Payable",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"false"
						],
						"Payable": 1383.2,
						"NumberOfInstalments_type_info": [
							"NumberOfInstalments",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"0",
							"1",
							"true"
						],
						"NumberOfInstalments": 12,
						"Name_type_info": [
							"Name",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"true"
						],
						"Name": "Monthly - Bank account",
						"InstalmentAmount_type_info": [
							"InstalmentAmount",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"0",
							"1",
							"true"
						],
						"InstalmentAmount": 115.27,
						"field_order_type_info": [
							"Code",
							"InstalmentAmount",
							"Name",
							"NumberOfInstalments",
							"Payable"
						],
						"Code_type_info": [
							"Code",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"true"
						],
						"Code": "PREMFUND12BA",
						"apex_schema_type_info": [
							"http://services.fastr.com.au/Quotation/Data",
							"true",
							"false"
						]
					},
					{
						"Payable_type_info": [
							"Payable",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"false"
						],
						"Payable": 1383.2,
						"NumberOfInstalments_type_info": [
							"NumberOfInstalments",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"0",
							"1",
							"true"
						],
						"NumberOfInstalments": 12,
						"Name_type_info": [
							"Name",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"true"
						],
						"Name": "Monthly - Credit Card",
						"InstalmentAmount_type_info": [
							"InstalmentAmount",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"0",
							"1",
							"true"
						],
						"InstalmentAmount": 115.27,
						"field_order_type_info": [
							"Code",
							"InstalmentAmount",
							"Name",
							"NumberOfInstalments",
							"Payable"
						],
						"Code_type_info": [
							"Code",
							"http://services.fastr.com.au/Quotation/Data",
							null,
							"1",
							"1",
							"true"
						],
						"Code": "PREMFUND12CC",
						"apex_schema_type_info": [
							"http://services.fastr.com.au/Quotation/Data",
							"true",
							"false"
						]
					}
				],
				"field_order_type_info": [
					"PaymentMethod"
				],
				"apex_schema_type_info": [
					"http://services.fastr.com.au/Quotation/Data",
					"true",
					"false"
				]
			},
			"apex_schema_type_info": [
				"http://services.fastr.com.au/Quotation/Data",
				"true",
				"false"
			]
		},
		"ServiceContractPremium_type_info": [
			"ServiceContractPremium",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"ServiceContractPremium": null,
		"QuotationNumber_type_info": [
			"QuotationNumber",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"QuotationNumber": "Q14327612",
		"QuotationGenerated_type_info": [
			"QuotationGenerated",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"false"
		],
		"QuotationGenerated": true,
		"MotorPremium_type_info": [
			"MotorPremium",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"MotorPremium": null,
		"LtiPremium_type_info": [
			"LtiPremium",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"LtiPremium": null,
		"LgiPremium_type_info": [
			"LgiPremium",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"LgiPremium": null,
		"IsiPremium_type_info": [
			"IsiPremium",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"IsiPremium": null,
		"InsurerQuotationUrl_type_info": [
			"InsurerQuotationUrl",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"InsurerQuotationUrl": null,
		"GapPremium_type_info": [
			"GapPremium",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"GapPremium": null,
		"field_order_type_info": [
			"CciPremium",
			"FailureReasons",
			"GapPremium",
			"InsurerQuotationUrl",
			"IsiPremium",
			"LgiPremium",
			"LtiPremium",
			"MotorPremium",
			"QuotationGenerated",
			"QuotationNumber",
			"ServiceContractPremium",
			"TotalAssistPremium",
			"TruckGapPremium",
			"VehicleDescription",
			"WarrantyPremium"
		],
		"FailureReasons_type_info": [
			"FailureReasons",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"FailureReasons": null,
		"CciPremium_type_info": [
			"CciPremium",
			"http://services.fastr.com.au/Quotation/Data",
			null,
			"0",
			"1",
			"true"
		],
		"CciPremium": null,
		"apex_schema_type_info": [
			"http://services.fastr.com.au/Quotation/Data",
			"true",
			"false"
		]
	}
]
I am already able to pull values and transfer them to Apex objects for fields like QuotationNumber.

I am having some difficulty with parsing being attempted with items in the JSON like StampDuty. 

This is just returning a null value at the moment. 

I created this additional class to contain the value and used readValueAs to process the data into Apex.
public class TotalAssistPremium {
		//public List<String> StampDuty_type_info;
		public Double StampDuty;
		//public List<String> Premium_type_info;
		//public Integer Premium;
		//public List<String> Gst_type_info;
		//public Double Gst;
		//public List<String> field_order_type_info;
		//public List<String> EstablishmentFee_type_info;
		//public Integer EstablishmentFee;
		//public List<String> CoverTypeCode_type_info;
		//public String CoverTypeCode;
		//public List<String> CostExGst_type_info;
		//public Integer CostExGst;
		//public List<String> AvailablePaymentMethods_type_info;
		//public AvailablePaymentMethods AvailablePaymentMethods;
		//public List<String> apex_schema_type_info;
		
		public TotalAssistPremium(Double sTDty) {
		
		    StampDuty = sTDty;
		    
		}    
		
	}
This one is still nulling for me as I try to check on the StampDuty item from the JSON.
TotalAssistPremium tAsstPrem = (TotalAssistPremium)parser1.readValueAs(TotalAssistPremium.class);
Help on this is appreciated.
Best Answer chosen by Savvy Admin
Savvy AdminSavvy Admin
Hi Alain,

I appreciate your reply. 

I have resolved this and just did the current token, while loop to pull the JSON value. 

All Answers

Alain CabonAlain Cabon
Hello,

Have you tried some online tools like
  • json2apex    
    https://www.adminbooster.com/tool/json2apex
  • This online tool can take a JSON string and convert it into an Apex Code class. 
  • You only need to copy/paste the resulting code into your Salesforce, force.com or database.com org. 
  • You can change the Class Name before generating the Apex code. 
  • This tool is secure as no data is transmited over Internet. 
   
Give us feedback on these tools (limitations). What would the best online tool have been for your need?

Best regards

Alain
Savvy AdminSavvy Admin
Hi Alain,

I appreciate your reply. 

I have resolved this and just did the current token, while loop to pull the JSON value. 
This was selected as the best answer