Skip to content

Deserialising object attributes #98

Open
@andrewn

Description

@andrewn

I'm working with an API that returns an attribute that is an object e.g. interest:

{
  "data": [
    {
      "type": "accounts",
      "attributes": {
        "accrued_interest": 0,
        "interest": {
          "rate_terms": "FIXED",
          "charge_frequency": "ANNUALIZED",
        }
      },
    }
  ]
}

I'm using ES6 to create a JSORM model:

const Account = Base.extend({
  static: {
    jsonapiType: 'accounts'
  },
  attrs: {
    accruedInterest: attr(),
    interest: attr(),
  }
});

(Base is a JSORMBase I've extended to centralise the API endpoint and Auth config.)

Is there a way for JSORM to parse the interest attribute, so that model.interest.rateTerms would be available? In that example above, it's only available as model.interest.rate_terms.

I've also tried interest: attr({ type: Interest }) where Interest is a class I've created to contain this data.

A pointer to the recommended way of doing this would be very helpful. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions