Write a function that accepts an API response formatted as a JSON string. This function should parse, validate, and map the response JSON based on the following specifications.
Validation of the response should be checked using the value of Response.Status. If the Status value is not 'Success', then an error should be thrown. A reason string for the response failure can be found at Response.Reason and should be included with the thrown error's mesasge.
If the response is considered successful, map the result to the following object specification:
report: The base object containing the reportreport.personal: A collection of personal informationreport.personal.address: The most recent address of the consumer, taken fromResponse.Results[].Addresses[], where the most recent date is determined by theMoveInDateofResponse.Results[].Addresses[]report.personal.address.street: The combination ofStreet1andStreet2of the most recent address of the consumerreport.personal.address.city: The city of the most recent address of the consumerreport.personal.address.state: The state of the most recent address of the consumerreport.personal.address.postalCode: The zip of the most recent address of the consumerreport.accounts[]: A list of accounts for the consumer, taken fromResponse.Results[].Accounts[]report.accounts[].accountId: The id for the accountreport.accounts[].balance: The current balance for the account, in a numeric formatreport.accounts[].currency: The 3-digit ISO currency code for the accountreport.accounts[].status: The current status for the account, mapped according to the given "Account Statuses" mappings belowreport.summary: The summary for the reportreport.summary.balance: The sum of all balances of all accountsreport.summary.openAccounts: The count of all accounts which have a mapped status of'open'
Response Statuses
SuccessFailed
Account Status Mappings
Active->openCancelled->closedOverdrawn->open
{
"Response": {
"Client": "Nova",
"Version": "10.4.23",
"Status": "Success",
"Results": [
{
"Addresses": [
{
"MoveInDate": "2010-01-01",
"Street1": "321 First Avenue",
"Street2": "",
"City": "Destination Town",
"State": "CA",
"Zip": "54321"
},
{
"MoveInDate": "2011-04-23",
"Street1": "123 Somewhere St",
"Street2": "Apt 7A",
"City": "Placeville",
"State": "CA",
"Zip": "12345"
},
{
"MoveInDate": "Unknown",
"Street1": "999 Other Road",
"Street2": "",
"City": "Upriver",
"State": "CA",
"Zip": "66666"
},
{
"MoveInDate": "2012-08-03",
"Street1": "123 Somewhere St",
"Street2": "Apt 5B",
"City": "Placeville",
"State": "CA",
"Zip": "12345"
}
],
"Accounts": [
{
"ID": "11111",
"Currency": "USD",
"Balance": "123.65",
"Status": "Active"
},
{
"ID": "22222",
"Currency": "USD",
"Balance": "0.0",
"Status": "Cancelled"
},
{
"ID": "33333",
"Currency": "USD",
"Balance": "-4.53",
"Status": "Overdrawn"
}
]
}
]
}
}