Modify the JSON format for the API POST request

I need assistance with making an API POST call in Angular 8. The JSON object structure that needs to be sent should follow this format:

-{}JSON
  -{}data
     -[]exp
        +{} 0
        +{} 1

However, the data I am sending is currently in this format:

-[]JSON
   +{} 0
   +{} 1

Upon receiving two objects {}`0, {}1 in an array named receivedData, I am storing the data like so in my TypeScript code:

this.changedData = this.receivedData;
this.postService.postMethod(this.headers, this.changedData)

The postService method looks like this:

postMethod(header, changedData): Observable<any[]> {
return this.http.post<any>(`the url here`, changedData, {headers: header, responseType: 'text' as 'json'})
.pipe(map(response => {
return response;
}))
}

My question is how can I send the data in the desired format? I want the JSON structure of changedDetails to match the specified format with the same naming conventions such as: {}data and []exp. How can I push the receivedData objects into exp[], which can then be included in data{} and ultimately pushed into changedDetails {}.

Answer №1

To ensure we are on the same page, let's assume you are getting data in this format:

[ { ... },  { ... } ]

And your goal is to convert it into this format:

{
  data: {
    exp: [ { ... }, { ... } ]
  }
}

(Please inform me if my assumption is incorrect.) If I am correct, then the conversion process is quite simple: just create a new object literal as shown below:

this.updatedData = {
  data: {
    exp: this.receivedData,
  },
};

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

Require fields in TypeScript interfaces only for array types

Is there a way to make only array type interface fields required, not all of them? The Required operator currently makes every field mandatory, but I specifically need just the array fields to be required. ` interface IExample { a: number, b?: str ...

Javascript Library Issue: "Implicitly Declared Type 'Any' Error"

I am currently in the process of developing a JavaScript library that will interact with an API. My goal is to create a module that can be easily published on npm and utilized across various frameworks such as Angular or React. Below is the code snippet fo ...

Angular: Navigating through two levels of fetched data from Firebase

I'm currently working on parsing retrieved data from Firebase within an Angular (Typescript) project. The structure of my JSON data in Firebase resembles the following: "customer" : { "customerId1" : { "documents" : { "documentId1" : { ...

What could be the reason for my router navigate function not functioning properly in Angular 8?

I need help with redirecting to another component in my Angular application. Currently, I have the following code: HomeComponent checkUrl(reference) { if (reference != this.ref) { this.router.navigate(['/еrror']); } } Thi ...

Cease the (pojo) serialization process if any of the mandatory fields are found to be null

Imagine this scenario with a custom Java object: import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonInclude.Include; @JsonInclude(content=Include.NON_NULL ...

Form with checkboxes in a Next.js and Typescript application

I am currently working on a project using Next.js and Typescript. I have implemented a form in this project, which is my first experience with Typescript and checkbox types. However, I am encountering difficulties in retrieving all checkbox values, adding ...

`MongoDb aggregation performance degradation with numerous collections (join)`

I am currently working on a project using the MEAN stack and I have noticed that I am utilizing a significant number of collections in my aggregation, resulting in a heavy reliance on lookup. This has had a negative impact on performance, causing the execu ...

Make sure to wait for the store to finish updating the data before accessing it. Utilize RxJS and Angular

Greetings! I am currently working with Angular and RxJS, and I'm trying to find a solution to wait for the store's data to be updated after an action is dispatched in order to perform some operations using that data. Below you can see a snippet o ...

When attempting to import the image path from a JSON file, a ReferenceError occurs stating that the data variable is not

I'm currently attempting to iterate through image paths in a JSON file and display them in a browser using the "img" tag. While hardcoded values work perfectly fine, I encountered an issue when trying to switch to a variable as outlined in this post: ...

The paginator feature is not supported by mat-table

My mat-table seems to be ignoring the paginator setting: https://i.sstatic.net/HLJrI.png In the screenshot provided, all 7 people are displayed on the first page instead of the expected 5. I have followed various tutorials on implementing paginators, incl ...

Exploring the depths of a multidimensional dictionary within AngularJS

I am currently working on a project using AngularJS. The data I have is in the form of JSON: { "leagues":{ "aLeague":{ "country":"aCountry", "matchs":{ "aUniqueID1":{ "date":"2014-09-07 13:00:00", "guest_play ...

Interacting with child collections data in C# through a RESTful API using Entity Framework context

Utilizing projection (explicit loading) to fetch and filter collection data from a child table via a RESTful Api service results in the JSON repeating the child table collections object. However, using the "AsNoTracking()" method eliminates the nested chil ...

An easy guide to using validators to update the border color of form control names in Angular

I'm working on a form control and attempting to change the color when the field is invalid. I've experimented with various methods, but haven't had success so far. Here's what I've tried: <input formControlName="pe ...

Updating templates using Angular 2 observables for change detection

Looking to optimize performance, I am exploring the implementation of manual change detection on my components. The app structure is as follows: App -> Book(s) -> Page(s) In the AppComponent, I subscribe to an observable and then utilize the "markF ...

Ensure that the objection model aligns with the TypeScript interface requirements

I am currently working on implementing an API using express, objection.js, and TypeScript. I found a lot of inspiration from this repository: https://github.com/HappyZombies/brackette-alpha/tree/master/server/src Similar to the creator, I aim to have var ...

Exploring Angular 8: Connecting elements to an array filled with objects

My goal is to achieve the following: https://i.sstatic.net/TQeKN.png In my form, I have 2 fields: description and price. When the plus button is clicked, additional input fields (Input 2 and Price 2) are generated dynamically. I want to bind these field ...

Error Message: Unable to access properties of an undefined object while interacting with an API in a React application

Creating a Weather application in React JS that utilizes the OpenWeatherMapAPI to display dynamic backgrounds based on the API response. I need to access the data at 'data.weather[0].main' which will contain values like 'Clear', ' ...

What is the best way to access nested JSON data in Vue.js code demonstrated here?

How can I properly access the nested JSON data for stage.name provided in the example below? As shown in the template, my attempt to retrieve the stage name is not working. Using vue.js created() { url="http://{{ api_endpoint }}" fetch(url) ...

How can I convert JSON data to a List of classes and vice versa?

Hey everyone, I'm having trouble getting this to work. I checked on SO and thought I found a solution, but it's not working for me. Error message: The deserialized type should be a standard .NET type (not a primitive type like integer, nor a ...

Is it possible to pass a Styled Components Theme as Props to a Material UI element?

After spending 9 hours scouring the internet for a solution, I am at my wit's end as nothing seems to work. Currently, I am developing a React component using TypeScript. The issue lies with a simple use of the Material UI Accordion: const Accordion ...