Home > OS >  How can I change the Turkish characters and the space in the json data object names ? WebApi/Typescr
How can I change the Turkish characters and the space in the json data object names ? WebApi/Typescr

Time:12-24

I am using api fetch data. But my json data format has Turkish characters and space. Thats why I couldn't get the datas to datatable or anything. I tried replace or parse function but I have no result.

real 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"
    },

format to be

 {
        "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"
    },

CodePudding user response:

Pass the field names to below function -

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

CodePudding user response:

You should be able to use String.prototype.normalize() to remove the accented characters.

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

Check out Unicode Normalization Forms to get to know the difference between NFD and NFC

The normalize() function with NFD decomposes accented characters into the combination of simple ones. The ü becomes u ̈.

Next we do a regex replace to remove the U 0300 → U 036F range with contains all the Diacritical Marks.

To support the ı you might still have to do another regex replacement like replace(/\ı/g, "i")

so finally

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