I'm looking for a way to modify the Turkish characters and spaces in the names of JSON data objects. I plan to do this using WebApi

I am facing an issue with fetching data through an API. The JSON data format contains Turkish characters and spaces, causing problems when trying to display the data in a datatable. I have attempted to use the replace and parse functions, but so far, I have been unsuccessful.

Original JSON format:

 {
        "Kod Adı": "USD",
        "Alış Kür": "12.4448"
    },
    {
        "Kod Adı": "AUD",
        "Alış Kür": "8.8412"
    },
    {
        "Kod Adı": "DKK",
        "Alış Kür": "1.8851"
    },
    {
        "Kod Adı": "EUR",
        "Alış Kür": "14.0385"
    },
    {
        "Kod Adı": "GBP",
        "Alış Kür": "16.5046"
    },

Desired JSON format:

 {
        "KodAdi": "USD",
        "AlisKur": "12.4448"
    },
    {
        "KodAdi": "AUD",
        "AlisKur": "8.8412"
    },
    {
        "KodAdi": "DKK",
        "AlisKur": "1.8851"
    },
    {
        "KodAdi": "EUR",
        "AlisKur": "14.0385"
    },
    {
        "KodAdi": "GBP",
        "AlisKur": "16.5046"
    },

In addition to this, I am encountering a CORS policy error.

The request from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I have tried various methods suggested in this article, but none have yielded positive results.

shared.service.ts

getAnalizor() : Observable<any[]>{
  return this.http.get<any>('http://15.12.1.05/analizorJSON.php')
}
  

Answer №1

To utilize the function below, simply provide it with a list of field names -

function convertSpecialCharacters(text) {
  return text
    .replace(/\ğ/g, "g")
    .replace(/\ü/g, "u")
    .replace(/\ş/g, "s")
    .replace(/\ı/g, "i")
    .replace(/\ö/g, "o")
    .replace(/\ç/g, "c");
}

Answer №2

If you want to remove accented characters, you can utilize the String.prototype.normalize() method.

const str = "Alış Kür"
str.normalize("NFD").replace(/[\u0300-\u036f]/g, "")
> "Alıs Kur"

For more information on Unicode Normalization Forms like NFD and NFC, refer to Unicode Normalization Forms.

The normalize() function using NFD breaks down accented characters into separate simple components. For instance, ü is transformed into u + ̈.

We then use a regex replace operation to eliminate characters in the U+0300 → U+036F range, which encompasses all Diacritical Marks.

In order to account for the character "ı", you may need to perform an additional regex replacement like replace(/\ı/g, "i")

Therefore, the final code would look like:

const str = "Alış Kür"
str.normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/\ı/g, "i").replace(" ", "")
> "AlisKur"

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

Arrange the array in ascending order according to the months' values

I am trying to organize a PHP array that looks like this - $testar = array( '3423sdfskjx' => 'January 2017', '1233sd3dkjx' => 'December 2017', '1534grfdbfd' => 'March 2017', &apos ...

Tips for connecting to a docker container containing an Angular application while the Angular app is also running in its own container

I have encountered an issue with my application that consists of a frontend and a backend running together in a Docker compose container. The backend has been functioning well without any problems during development, but the frontend, which is an Angular a ...

After each save, gulp-typescript is emitting errors, however, it works without any issues upon subsequent saves

I'm facing some uncertainty regarding whether the issue I'm encountering is related to gulp, typescript, or Angular 2. Currently, I am using Angular 2 Beta 6. Here is an example of my typescript gulp task: var tsProject = p.typescript.createPr ...

What is the best way to represent JSON data within another object?

I am currently in the process of making an API call to fetch data and then mapping those objects to a model object. The JSON received contains objects within objects, which I have successfully mapped with the initial object. However, there is an array with ...

Demonstrate complete attendance by detailing specific days of presence and days of absence within a specified date range

In order to track the attendance of employees, I have a log that records when an employee is present or absent on specific dates within a given interval. Let's consider the following date range: $from = "2019-03-01"; $to = "2019-03-05"; Here is a ...

Is there a way for me to display information from a secondary child component by simply clicking a button on the main component instance?

Consider a scenario where there is a main instance, a child component named "movie-card," and another child component within "movie-card" called "link-btn." The objective is to create a selector that loops through the "link-btn" component using v-for. Addi ...

What is preventing me from setting a background image in Angular 13?

Trying a different approach based on advice from Stack Overflow, I attempted the following: <div [style.background-image]="'url(https://picsum.photos/200)'"></div> Unfortunately, this resulted in no effect and the image was ...

When I delete the initial element from the array, the thumbnail image disappears

Using react-dropzone, I am attempting to implement image drag and drop functionality. The dropped image is stored in the React state within a files array. However, a problem arises when removing an image from the array causing the thumbnails of the remain ...

What is the best way to conceal the parent ul when at the li level?

html component <div class="select-wrapper select"> <input (click)="toggleOptionsView()" type="text" class="select-dropdown" data-activates="select-options-524f0174-3e9b-445a-8bf3-e304572eb476" value="Choose your option"> <ul [n ...

Set the enumeration value to a variable

I am facing a problem where it seems impossible to do this, and I need help with finding a solution enum Vehicles { BMW='BMW', TOYOTA='Toyota' } class MyVehicles { public vehicleName: typeof Vehicles =Vehicles; } const veh ...

Receiving null value with Web API POST using [FromBody]

Below is the code for my WebAPI in C#: [Route("")] [HttpPost] public void SaveTestRun([FromBody] object data) { inputResultsToDatabase(data); } This is the ajax request I am making: sendTestData() { t ...

Using event.target to pass HTML form data to FormData is causing an error stating that the Argument of type 'EventTarget' cannot be assigned to a parameter of type 'HTMLFormElement'

Looking to extract data from a form and store it in FormData: const handleSubmit = (e: FormEvent<HTMLFormElement>) => { e.preventDefault(); const formData = new FormData(e.target as HTMLFormElement); const value = formData.get(' ...

Explore ways to incorporate special symbols in a jQuery array

I'm looking to include special characters in a jQuery array. I'm not quite sure how to do this. Currently, my code is: $scope.categories = ['Red', 'White', 'Rose', 'Sparkling']; and I would like it to be: ...

retrieving JSON data using ajax and processing it in PHP

I have some data that needs to be sent to the backend, and it looks like this: function view(){ let id = "12345678"; let profile = [{name:"dave", department : "Engineering"}, {name:"Tedd", ...

Changing a particular value within a JSON field in MongoDB using Mongoose on a Linux system

I am currently working on modifying a specific value within a json-type field. The structure of my document schema is as follows: character { name: {type: string}, experience: {type: json} ... } I always ensure that the 'experience&apos ...

Using unserialized elements in the View

Currently, I am facing an issue with accessing elements stored in an array within the PHP session. Whenever I try to directly access these elements from the view, I notice that the object is incomplete, and its properties cannot be accessed. To solve this ...

Having trouble with your Typescript *.ts files?

Having trouble understanding why the command tsc *.ts isn't functioning correctly. The error message TS6053: File '*.ts' not found keeps appearing. Any suggestions on how to compile all the .ts files within a directory? Thank you! ...

How can I rename an event function in Angular 2?

Is it possible to dynamically change the function associated with an event? I attempted to do so like this: (click) = "{{myFunction}}" However, I encountered an error stating "Parser Error: Got interpolation ({{}}) where expression was expected". I am lo ...

Can TypeScript be set up to include undefined as a potential type in optional chains?

Today, I encountered a bug that I believe should have been caught by the type system. Let me illustrate with an example: function getModel(): Model { /* ... */ } function processModelName(name: string) { return name.replace('x', 'y& ...

Is it possible to assign a variable to an Ionic datetime input property?

I am trying to pass a variable, someVar, into the max attribute (input property) of an Ionic 2 (and Angular 2) DateTime component. It seems like it only accepts a hardcoded string such as max="2017-08-31". HTML <ion-datetime displayFormat="DD/MM/YYYY" ...