generic mapper for json typescript

127 Views Asked by At

I want to filter only the key and values which is in the type of document model in typescript example below:

type DocumentModel = {
  id?: string | null,
  docType: DocType,
  accountId?: string | null,
  updatedBy: string | null;
};

whatever the JSON object is, I need to filter only these values in the output

input json

{
    "id": "80e474bc-7e28-4e28-ad43-c031da0f297b",
    "docType": "80e474bc-7e28-4e28-9b11-2259cf68f91e",
    "type": "INDIVIDUAL",
    "accountId": "80e474bc-7e28-4e28-ad43-c031da0f297b",
    "account": {
               "id": "80e474bc-7e28-4e28-ad43-c031da0f297b",
               "type": "INDIVIDUAL",
               "updatedAt": "2023-04-05T11:53:17.587Z"
                },
    "attributes": "{\"identity\":\"12345\",\"name\":\"nicky\"}",
    "questionnaire": null,
    "owner": "bddfe953-0c1c-4aa4-9b11-2259cf68f91e",
    "comments": null,
    "updatedBy": "bddfe953-0c1c-4aa4-9b11-2259cf68f91e",
    "createdAt": "2023-04-05T11:53:17.587Z",
    "updatedAt": "2023-04-05T11:53:17.587Z"
}

I want only the below output

{
"id": "80e474bc-7e28-4e28-ad43-c031da0f297b",
"docType": "80e474bc-7e28-4e28-9b11-2259cf68f91e",
"accountId": "80e474bc-7e28-4e28-ad43-c031da0f297b",
"updatedBy": "bddfe953-0c1c-4aa4-9b11-2259cf68f91e",
}
0

There are 0 best solutions below