Why does JSONResult automatically convert camelCase property names to lowercase?

Project Summary

I am currently engaged in a project that involves:

1- Angular 6

2- Aspnet Core 2.1

Situation

In my Angular code, I am making a GET request using the following snippet:

let reqHeader = new HttpHeaders({ 'Content-Type': 'application/json' });
  this.http.get(this._URL).subscribe(
    (data => {
      this._engineersList = data[0];
      this._shiftList = data[1];
      console.log(data[0])
    }), error => this.error
  );

class Engineers {
    public ID: number;
    public Name: string;
}

class Shifts {
    public ID: number;
    public EmployeeID: number;
    public EmployeeName: string;
    public ShiftName: string;
    public ShiftTime: string;
}

Since my properties are in Camel case, I bind them the same way in my HTML like so:

<tbody>
    <tr *ngFor="let engineer of _engineersList">
      <td>
        {{engineer.Id}}
      </td>
      <td>
        {{engineer.Name}}
      </td>
    </tr>
  </tbody>

I see no data binding as expected:

https://i.sstatic.net/KLMbJ.png

However, upon converting my Camel case properties to lowercase:

<tbody>
    <tr *ngFor="let engineer of _engineersList">
      <td>
        {{engineer.id}}
      </td>
      <td>
        {{engineer.name}}
      </td>
    </tr>
  </tbody>

https://i.sstatic.net/7DPlU.png

Discoveries

Upon examining the results of my GET request in the browser console, I noticed that the observable is returning properties in lowercase.

https://i.sstatic.net/4NANV.png

I also confirmed on the server side that there were no issues with the data being returned. The server provided the correct data with properties properly in Camel case.

https://i.sstatic.net/dggWg.png

My question is why the JSON result is displaying and binding properties in lowercase even though my model class has properties in Camel case? I have detailed my issue here and would appreciate any assistance or insights on what could be wrong in my code. Thank you for your help.

Answer №1

You can easily configure this by adding the code below:

public void ConfigureServices(IServiceCollection services)
{

    services.AddMvc()
        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
}

Answer №2

Have you experimented with the middleware attributes? Perhaps you're seeking to enforce either Camel or Pascal Case here?

protected void Application_Start()
{
    HttpConfiguration configuration = GlobalConfiguration.Configuration;
    configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    configuration.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
}

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

What are the steps to turn off the color display of the "ng build" command?

Is there a method to deactivate the colorized output of the Angular CLI ng build command? Specifically, I am looking to disable the colorful summary that is displayed on the console upon completion. I'm hoping for a solution such as an environment var ...

Running into issues compiling Ionic 4 with Angular 7 inside a Docker container

Facing Issues Compiling Ionic 4 with Angular 7 in Docker I am trying to compile a project using Ionic 4 and Angular 7 within Docker. Here are the steps I have taken: I manually created an image with Java JDK version 8, following this article How To Ins ...

Having trouble accessing a specific array index in a JSON response from the Quandl API using Google Apps Script because it's showing as "undefined"

My experience with JSON is limited, mainly using Linux command line tools in Python. I am currently working on a project with Google Apps Scripts where I need to retrieve specific data from the Quandl API. In this case, I am interested in extracting the "C ...

Creating a data type restricted to utilizing property names exclusively from a specified string union:

I have a specific Enum: enum MyEnum { optionOne = 0, optionTwo = 1, optionThree = 2, optionFour = 3, } and a related Type: export type EnumNamesList = keyof typeof MyEnum; I am looking to create a type similar to this: export type EnumDataTypes = ...

Employing the keyof operator with the typeof keyword to access an object

What data type should be used for the key variable? I am encountering an error stating that "string" cannot be used to index the type "active1: boolean, active2". const [actives, setActives] = React.useState({ active1: false, active2: false, }); con ...

Displaying the preselected option in a Select dropdown menu using Angular

Here is the code snippet I have: <select label="people" id="ppl" [(ngModel)]="Selectedppl" (ngModelChange)="onPplSelection($event.target.value)"> <option>select people</option> <option *ngFor="let x of peopleList" [ngValue]="x"> ...

Error in VS2015 when attempting to assign a two-dimensional array in TypeScript build

I've run into some build errors in my TypeScript project in Visual Studio 2015. Despite the application working fine in the browser, I'm unable to publish it due to these errors. export var AddedFields: Array<Array<Field>[]>[]; myGl ...

Show all items in the RecyclerView, but limit the display to 50 items unless the search bar is

Is there a way to load all items from a JSON List RecyclerView using Volley but only display a certain number of items (e.g. 50) while still being able to search the entire JSON data with a search tool? I have tried to limit the number of items displayed ...

Accessing JSON data in PHPWould you like a

I'm currently dealing with a JSON array where I need to access a specific value at a lower level and print it. The issue arises when I encounter levels indicated by [0] (or [n]). For instance, I have the following output and I want to only display the ...

Prevent a specific file from being compiled during karma testing sessions

For my Angular application using angular-cli with Karma, I am facing an issue where a specific file needs to be excluded from compilation during unit tests. The file causing the problem is mentioned in the error message, indicating that it cannot be compi ...

The use of cURL to send a POST array is resulting in an error message stating "Invalid

I am attempting to send an array of data from one domain to another using cURL. When I run the code below, I encounter the error message "Invalid argument supplied for foreach()". I have a suspicion that the issue lies with the array after it has been deco ...

Restricting access to tabPanel in tabView when a tab is clicked using Angular

In my tabview, I have multiple tabpanels and I am able to programmatically control it using the [activeIndex] property. However, I am facing an issue where I want to display an alert and deny access to a specific tab if a certain variable is set to false. ...

What is the method for obtaining the most up-to-date JSON GET request URL?

Using JQGrid with search filters and setting loadOnce=false. I perform a search in the grid and observe the JSON request URL in firebug: http://localhost:8080/myapp/items/listGrid?ticketId=&_search=true&nd=1393573713370&rows=20&page=1& ...

Unable to simulate a service and retrieve information in an angular unit test - showing as undefined

In my current project, I have a component that I am trying to write unit tests for. However, when I run the test, I noticed that the value of console.log('**fav**' + favorite[`isFavorite`]); shows up as undefined. This indicates that the data I ...

Angular material dialog box experiencing issues with saving multiple values

Currently, I am utilizing Anular9 and implementing mat-raised-button in the following manner: <button mat-raised-button (click)="saveClick()" color="primary"><mat-icon>check</mat-icon>&nbsp;&nbsp;Ok</butto ...

"Exploring Angular 9: A guide to retrieving form data with an array of objects [Revised as of July 29th, 2020

I am encountering an issue with my Angular 9 form code. I am getting the error "ERROR TypeError: Cannot read property 'mobile_number' of undefined" and I need help in resolving this problem. <form (ngSubmit)="processForm()"> & ...

What is the process for moving data from the store to the form?

When retrieving data from the store, I typically use the following method: ngOnInit(): void { this.store.pipe(select(getPerson)) this.store.dispatch(loadPerson()); } However, I am now faced with the challenge of transferring this data from the ...

Struggling with updating json files

I have two json files. One of them is a dictionary which is a subset of the other. json_file_1.json contains {'apple': 5, 'banana': 7, 'pear': 8} json_file_2.json contains {'apple': 50, 'banana': 70}. I ...

During the iteration of a foreach loop, there is a specific point where the number being examined experiences a sudden increase in

During a "foreach" loop, I am incrementing a number in an attribute value. Each key in the array has values ranging from 0 to 3, and I need the number to repeat from 0 to 3 until all objects are processed. The original JSON data contains multiple products ...

Issue with Django and Angular 4 - The requested resource is missing the 'Access-Control-Allow-Origin' header

This is my django code snippet Whenever I test my delete function, this error occurs: The requested resource does not include the 'Access-Control-Allow-Origin' header. This means that the origin is not permitted access. The response resulted ...