Extract data from a JSON object using targeted translations

When responding to my client, I have data in a JavaScript format. The table I am loading requires specific field names. Is there a way to map a key in the JSON to a field name in the JSON object?

This is my current approach:

this.resArray = Array;
this.results = new this.resArray();  // This object uses 'Phone' instead of 'phone' 
var dataFromServerJSON = $.parseJSON(dataFromServer);  // Data from server includes phone:3127789342
$.merge(app.Client.view.results, dataFromServerJSON)

Answer №1

It seems like the structure of your JSON response needs some adjustments:

[
     {
         "title": "The Lion King",
         "releaseYear": "1994"
     },
     {
         "title": "Finding Nemo",
         "releaseYear": "2003"
     }
]

To correct this, your JSON object should be formatted like this:

[
     {
         "title": "The Lion King",
         "releaseYear": "1994"
     },
     {
         "title": "Finding Nemo",
         "releaseYear": "2003"
     }
]

To achieve this transformation, iterate through the response and assign values to a new JSON object initialized as newObj = []:

var newObj = [];
for (var i = 0; i < response.length; i++) {
    newObj[i] = {};
    newObj[i]["title"] = response[i]["title"];
    newObj[i]["releaseYear"] = response[i]["releaseYear"];
}

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

Error: Unable to update Ember Array - Property 'destroy' cannot be read because it is undefined

Is there a way to efficiently update an Ember Array so that changes in the array reflect in the view? Take a look at this simplified code snippet - cacheArr: Em.A([]), count: 1, updateFxn: function() { this.incrementProperty(count); devObj = Embe ...

Placing a Fresh Item into a Designated Slot within an Array

Imagine having a MongoDB collection that consists of an array of objects being retrieved from an Angular Resource. [{_id: "565ee3582b8981f015494cef", button: "", reference: "", text: "", title: "", …}, {_id: "565ee3582b8981f015494cf0", button: "", ref ...

Replicate the functionality of a backend API using AngularJS

Currently, I am in the midst of creating a GUI for an application that is still undergoing API development. Although I have a vision of how it will look, it lacks functionality as of now. Hence, I need to replicate its behavior until the API is fully funct ...

Creating instances for null objects using WebAPI JSON serialization

When sending JSON models to WebAPI controller methods, I have noticed that if there are null objects in the JSON model, the model binder will create instances of these items instead of leaving them as null on the server side object. This is different from ...

Using Angular as a template engine: A simple guide

My goal is to utilize Angular as a template engine and then pass the resulting HTML code to another library. In my template file named template.html: <div><h1><span data-ng-show="details.rs">{{details.rs}}</span></h1></di ...

When utilizing GSON for serialization, remember to incorporate the object type

If we consider having some basic types: public class Base { String a = "a"; Nested nested = new Nested() } public class Nested { String b = "b", } When using GSON to serialize this, the output would be: { "a": "a", "nested": { " ...

Drop and drag to complete the missing pieces

Here is a drag and drop fill in the blanks code I created. The challenge I'm facing is that I want users to be able to correct their mistakes by moving words to the right place after receiving points. <!DOCTYPE HTML> <html> <head&g ...

Determining the best application of guards vs middlewares in NestJs

In my pursuit to develop a NestJs application, I aim to implement a middleware that validates the token in the request object and an authentication guard that verifies the user within the token payload. Separating these components allows for a more organi ...

javascript menu.asp

Is there a method to access a specific MenuItem element within an asp:Menu using JavaScript? I am trying to dynamically change the imageUrl path when a user hovers over the menu. <div align="center"> <asp:Menu ID="Menu1" runat="server"& ...

What is the best way to setup a function parameter to store responses from an inquirer.prompt inquiry using JavaScript and integrating with MySQL?

How can I incorporate function parameters to handle answers from an inquirer.prompt question in JavaScript? While I already know how to achieve this without using variables, I aim to enhance the usability of my addToTable function by utilizing parameters ...

Remove all HTML tags except for those containing a specific class

Looking for a regex that removes all HTML tags except the "a" tags with the "classmark" class For example, given this HTML string: <b>this</b> <a href="#">not match</a> <a href="#" target="_blank" ...

A JointJS element with an HTML button that reveals a form when clicked

How do I bind data to a cell and send it to the server using the toJSon() method when displaying a form on the addDetail button inside this element? // Custom view created for displaying an HTML div above the element. // ---------------------------------- ...

Tips for incorporating the useContext hook into Next JS using .tsx files?

I am attempting to incorporate a dual theme (dark & light) in Next JS using MUI with the useContext method. Encountering this error: ⨯ src\app\page.tsx (6:50) @ useThemeContext ⨯ TypeError: (0, _providers_theme_context_provider__WEBPACK_IM ...

Please provide either a single integer or a list of integers using the same parameter

I've encountered an issue while developing a RESTful API. I need to be able to add/remove items to/from a group by sending a POST request to a specific URL. The request format is already established, but I'm facing a challenging requirement. The ...

Recreate body content with JQuery Mobile

I've been attempting to duplicate the entire HTML content within the tags. However, even though the appearance on screen is correct, none of the links are functioning.</p> <p>To view the fiddle, click here: [Here is a fiddle][1]</p> ...

Calculate the average color of the outer pixels in an image

I am looking to dynamically set the background color of a div using the average color of the outer pixels in an image. This way, I can avoid manually setting the background color for each image. For example, if the image is 100px x 100px, I would check th ...

Refresh the calendar to retrieve updated information

Utilizing the "events" elements of fullcalendar to retrieve data dynamically has been successful so far and I am satisfied with it. However, I am facing a challenge in passing a GET/POST parameter to the PHP page and refreshing the call to incorporate the ...

Struggling to accurately display JSON data on a world map using leaflet.js

I successfully designed a world map using leafletjs, and now I am working on displaying data from https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/meteorite-strike-data.json as circles on the map. The distribution of my output dat ...

Setting minimum and maximum limits for a JavaScript variable: tips and tricks

Currently, I am implementing a pagination feature using vuejs to display my algolia data. I am determining whether the user has clicked on either the previous button, next button, or directly inputted a page number (1, 2, 3, etc.) setPage: function(p ...

Opt for the modal class over the id for improved functionality

How can I use this modal multiple times on the same page when it only works once due to the id element? Can someone assist me in making it work multiple times on the same page? I have tried the following code, but when I click the button, nothing happens. ...