Casting types of objects in Angular2 using TypeScript

Trying to extract the model object from the JSON obtained through a http response call.

The following JSON represents the body of the http response,

{

"dataType": "RWSupplier",
"partyName": "Lifecare Pharmaceuticals",
"partyShortName": null,
"partySecondaryName": null,
"partySecondaryShortName": null,
"sortingName": "Lifecare Pharma",
"mailingName": null,
"entityType": "SP",
"partyType": "OG",
"partySubType": null,
"extPartyId": null,
"comments": null,
"prGenderType": null,
"prBirthDate": null,
"prSalutation": null,
"statusCode": null,
"lastUpdateDate": "2017-03-06T04:30:00.000+0000",
"lastUpdateUser": "RwAdmin",
"tinNum": "33450701833",
"cstNum": "790052 dt. 4/4/2001",
"stNum": null,
"dlNum1": "5324/MIII/20B",
"dlNum2": "5205/MIII/21B",
"limitList": [ ],
"tagList": [ ],
"buId": "510",
"partyId": "SP001011001" }

An interface has been created for the above JSON data as shown below,

export interface SupplierBrief {
dataType: string;
partyId: string;
buId: string;
partyName: string;
statusCode: string;
comments: string; }

Not all properties from the rest call are needed.

To optimize caching and reduce memory usage, unnecessary properties need to be omitted. The service is designed as follows,

return this.http.request(path, requestOptions).map((response: Response) => {
            return response.json() as models.SupplierBrief;
        });

However, the SupplierBrief still includes all properties returned by the rest call.

There might be a misunderstanding with the concept of model objects. Any corrections or clarifications would be appreciated.

Answer №1

Sorry, the responsibility lies with you. Generate a fresh object, assign the necessary values, and then submit it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Cracking the Code: Unleashing the Art of Parsing JSON with Dart's Dynamic Object

Can anyone help me with mapping this JSON in Dart? I received this JSON as the response.data from a get request made using the Dio library in Flutter. [ { "getPlayersFilterResult": { "Players": [{p1},{p2},..] } }, ...

Utilizing TypeScript with Vue3 to Pass a Pinia Store as a Prop

My current stack includes Typescript, Pinia, and Vue3. I have a MenuButton component that I want to be able to pass a Pinia store for managing the menu open state and related actions. There are multiple menus in the application, each using the same store f ...

Ensuring a Generic Type in Typescript is a Subset of JSON: Best Practices

I am interested in achieving the following: interface IJSON { [key: string]: string | number | boolean | IJSON | string[] | number[] | boolean[] | IJSON[]; } function iAcceptOnlyJSON<T subsetof IJSON>(json: T): T { return json; ...

Decoding a JSON timestamp in the Revel framework

I am currently working on parsing a Datetime in revel from a json request. The structure of the request is as follows: { "startedAt": "2017-06-01 10:39", ... } The struct it's being decoded into is defined like this: type MyStruct struct { St ...

Implementing conditional asynchronous function call with identical arguments in a Typescript React project

Is there a way in React to make multiple asynchronous calls with the same parameters based on different conditions? Here's an example of what I'm trying to do: const getNewContent = (payload: any) => { (currentOption === myMediaEnum.T ...

What is causing the error when trying to parse a JSON with multiple properties?

Snippet: let data = JSON.parse('{"name":"dibya","company":"wipro"}'); Error Message : An error occurred while trying to parse the JSON data. The console displays "Uncaught SyntaxError: Unexpected end of JSON input" at line 1, character 6. ...

Using a React component with Material-UI style classes in TypeScript is not possible

Recently delving into react, I've embarked on a learning project utilizing typescript 3.7.2 alongside material-ui 4.11.0 and react 16.13.1. Initially, I structured my page layouts using functional components, but upon attempting to switch them to clas ...

Struggling to translate JavaScript code into Typescript

Currently in the process of converting my JavaScript code to Typescript, and encountering an error while working on the routes page stating Binding element 'allowedRoles' implicitly has an 'any' type. ProtectedRoutes.tsx const Protecte ...

Tips for modifying JSON property names during the parsing process

As outlined in the JSON.parse documentation, a reviver function can be utilized to modify the value of each property within the JSON data. Here is an example: JSON.parse('{"FirstNum": 1, "SecondNum": 2, "ThirdNum": 3}', function(k, v) { return ...

Combining two arrays filled with objects to form a collection of Objects on a Map

In my JavaScript code, I'm receiving data from a WebService that looks like this: { "fire": { "totalOccurence": 2, "statsByCustomer": [ { "idCustomer": 1, "occurence": 1 }, { "idCustomer": 2, ...

What is causing the error message "TypeError: string indices must be integers" to appear in my code?

Upon reviewing the JSON file provided below, I encountered an error Traceback (most recent call last): File "test11.py", line 10, in <module> print(driver['id']) TypeError: string indices must be integers {"drivers": [ ...

Unraveling a String with an Array of Dictionaries in Swift through Decodable Protocol

The JSON response is:- { "id" = "1" "message" = "SUCCESS" "data" = "[{\"name\":"FirstName",\"office_id\":1111,\"days_name\": ...

A method for arranging an array of nested objects based on the objects' names

Recently, I received a complex object from an API: let curr = { "base_currency_code": "EUR", "base_currency_name": "Euro", "amount": "10.0000", "updated_date": "2024 ...

Retrieve recently appended DOM elements following the invocation of createComponent on a ViewContainerRef

I have a current function in my code that dynamically creates components and then generates a table of contents once the components are added to the DOM. This service retrieves all h3 elements from the DOM to include in the table of contents: generateDy ...

Issue encountered with Bing webmaster API when retrieving keyword statistics: an unknown error occurred resulting in an empty

My goal is to retrieve keyword statistics through the bing webmaster API using JSON GET requests. The required parameters for this operation are as follows: List<KeywordStats> GetKeywordStats( string q, string country, //optional s ...

I have retrieved information from a JSON file and am now attempting to display the data in my view (directory.php). However, I am encountering an error message that reads: "order:not

I have encountered an issue where fetching JSON data is causing problems, whereas defining an array works perfectly. Here is the code for a file directory.php that I am including with ng-view in my main index file using routing: ...

Unable to start an expo project in bare workflow using TypeScript

Can someone help me with setting up an expo bare workflow using TypeScript? I ran the command "expo init [project name]" in my terminal, but I can't seem to find the minimal (TypeScript) option. ? Choose a template: » - Use arrow-keys. Return to sub ...

Tips for properly defining path names in loadChildren for lazy loading in Angular 2 NgModules

When setting correct path names for loadChildren in the app-routing.module file within an Angular 2 NgModule, I encountered some issues. Despite following the NgModule concept outlined on the Angular main website, I still couldn't find clear informati ...

Troubles with Typescript typings when including an empty object in an array with specific typings

, I am facing a dilemma with displaying houses in a cart. Each house has an image, but since they load asynchronously, I need to show empty cards until the data is fetched. Initially, I added empty objects to the array representing the houses, which worked ...

Displaying jsp variable in the browser console

When working in JSP, using <p>${ob}</p> to retrieve an object can be challenging due to the size of the object. Is there a way to utilize JavaScript to print it instead? Keep in mind that since this involves handling email content, JavaScript ...