Skip to content

Instantly share code, notes, and snippets.

@robertbenjamin
Created March 31, 2020 17:21
Show Gist options
  • Save robertbenjamin/c0fc415e7adfc577bea03ea3e248f8d0 to your computer and use it in GitHub Desktop.
Save robertbenjamin/c0fc415e7adfc577bea03ea3e248f8d0 to your computer and use it in GitHub Desktop.
Parse and Map Report

Prompt

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 report
  • report.personal: A collection of personal information
  • report.personal.address: The most recent address of the consumer, taken from Response.Results[].Addresses[], where the most recent date is determined by the MoveInDate of Response.Results[].Addresses[]
  • report.personal.address.street: The combination of Street1 and Street2 of the most recent address of the consumer
  • report.personal.address.city: The city of the most recent address of the consumer
  • report.personal.address.state: The state of the most recent address of the consumer
  • report.personal.address.postalCode: The zip of the most recent address of the consumer
  • report.accounts[]: A list of accounts for the consumer, taken from Response.Results[].Accounts[]
  • report.accounts[].accountId: The id for the account
  • report.accounts[].balance: The current balance for the account, in a numeric format
  • report.accounts[].currency: The 3-digit ISO currency code for the account
  • report.accounts[].status: The current status for the account, mapped according to the given "Account Statuses" mappings below
  • report.summary: The summary for the report
  • report.summary.balance: The sum of all balances of all accounts
  • report.summary.openAccounts: The count of all accounts which have a mapped status of 'open'

Lookups

Response Statuses

  • Success
  • Failed

Account Status Mappings

  • Active -> open
  • Cancelled -> closed
  • Overdrawn -> open

Example Response JSON

{
  "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"
          }
        ]
      }
    ]
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment