Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help converting a complex json object #60

Closed
iangregsondev opened this issue Apr 8, 2019 · 5 comments
Closed

Help converting a complex json object #60

iangregsondev opened this issue Apr 8, 2019 · 5 comments
Assignees
Labels

Comments

@iangregsondev
Copy link

Hi,

I have a json object, its not complex but what I want to end up, is a little difficult for me to understand how to do this. The standard JSON object (represented by a typescript interface) is this

{
  "_embedded": {
    "record": {
      "name": "CompanyA",
      "meta": {
        "address": "6 some street",
        "stateCountry: "NY, USA"
      }
    }
  },
  "pageData": {
    "pagination" : {
      "page": 1,
      "totalPages": 1
    }
  }
}

I want to end up with the following, represented by a typescript class

export class Item {
   name: string // this comes from __embedded/record/name
   address: string // this comes from __embedded/record/meta/address
   state: string // this comes from __embedded/meta/stateCountry (split by coma, FIRST part)
   country: string // this comes from __embedded/meta/stateCountry (split by coma, SECOND part)
  pagination : Pagination 
}

export class Pagination {
    page: number //this comes from __embedded/pageData/pagination/page
    totalPages: number //this comes from __embedded/pageData/pagination/totalPages
    hasNext: boolean // this uses the above 2 properties to work out if there is a nextPage, true/false
}

Any help really appreciated

Thanks

@emyann
Copy link
Member

emyann commented Apr 8, 2019

Hello @appsolutegeek, thank for using Morphism!

You can achieve what you want using 2 similar approaches:

  • Using 1 single schema transforming the source to the Item interface
  • Break down your transformations by using 2 nested schemas; 1 schema from source to Pagination and a parent schema from source to Item

Considering the source is:

const source = {
  "_embedded": {
    "record": {
      "name": "CompanyA",
      "meta": {
        "address": "6 some street",
        "stateCountry": "NY, USA"
      }
    }
  },
  "pageData": {
    "pagination": {
      "page": 1,
      "totalPages": 1
    }
  }
}

With 1 single complex schema

const complexSchema = createSchema<Item, typeof source>({
  name: '_embedded.record.name',
  address: '_embedded.record.meta.address', 
  state: ({ _embedded: { record: { meta: { stateCountry } } } }) => stateCountry.split(',')[0], 
  country: ({ _embedded: { record: { meta: { stateCountry } } } }) => stateCountry.split(',')[1], 
  pagination: { 
    page: 'pageData.pagination.page', 
    totalPages: 'pageData.pagination.totalPages', 
    hasNext: ({ pageData: { pagination } }) => !(pagination.page === pagination.totalPages) 
    }
})

morphism(complexSchema, source)

With 2 nested schemas

const paginationSchema: StrictSchema<Pagination, typeof source> = { 
    page: 'pageData.pagination.page', 
    totalPages: 'pageData.pagination.totalPages', 
    hasNext: ({ pageData: { pagination } }) => !(pagination.page === pagination.totalPages) 
}
const nestedSchema = createSchema<Item, typeof source>({
  name: '_embedded.record.name',
  address: '_embedded.record.meta.address', 
  state: ({ _embedded: { record: { meta: { stateCountry } } } }) => stateCountry.split(',')[0], 
  country: ({ _embedded: { record: { meta: { stateCountry } } } }) => stateCountry.split(',')[1], 
  pagination: morphism(paginationSchema) // Map the Pagination object using the schema above
})

morphism(nestedSchema, source)

Here's a working playground if you want to test it: https://stackblitz.com/edit/morphism-single-source-to-complex-destination

Is it what you were looking for ? Please let me know if there are some parts where you need further explanation.

@emyann emyann self-assigned this Apr 8, 2019
@emyann emyann added the question label Apr 8, 2019
@iangregsondev
Copy link
Author

That was perfect! Thank you!

@emyann
Copy link
Member

emyann commented Apr 9, 2019 via email

@iangregsondev
Copy link
Author

Sorry @emyann , I think that was a comment I left previously, I actually deleted it because sure enough, it was as easy as that.

Although it got it on iteretee

  totalPages: iteratee => iteratee.page.totalPages,

@emyann
Copy link
Member

emyann commented Apr 9, 2019

Perfect, thank you! Closing this one :)

@emyann emyann closed this as completed Apr 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants