Home > Blockchain >  Changing JSON value after parsing from API
Changing JSON value after parsing from API

Time:10-02

I'm parsing the following from:

    json.listMap.forEach((listItem: { State: string; Country: string})```

Response.data looks like this:
```{
  success: true,
  listMap: [
    {
      State : 'Northrine Westfalia',
      Country: 'Germany'
    },
    {
      {
      State : 'Bavaria',
      Country: 'Germany'
    }
    }
  ]
}

How can I convert the value in country currently being 'Germany' to 'Deutschland' after parsing it from the API using Java Script?

-> Let me be more open here. I don't really know that method I could use to do this. Maybe someone could point me at a documentation, of course I don't expect anyone to just hand me the code.

CodePudding user response:

Use Array.map after parsing JSON into an JavaScript array:

const list = response.listMap.map(entry => {
  return {
    State : entry.State,
    Country: entry.Country.replaceAll('Germany', 'Deutschland')
  }
})

Of course, if you need JSON structure, just convert final array into JSON string with the following:

const newJSONList = JSON.stringify(list);

CodePudding user response:

try this :

listMap = listMap.map(listMapItem => {
if ('Germany'===listMapItem.Country) {
    listMapItem.Country = 'Deutschland';
    return listMapItem;
} else {
    return listMapItem;
}
});

CodePudding user response:

assuming below is your response object.

var responsObj={  success: true,
listMap: [
    {
      State : 'Northrine Westfalia',
      Country: 'Germany'
    },
      {
      State : 'Bavaria',
      Country: 'Germany'
    }
  ]
};
responsObj.listMap=responsObj.listMap.map(x=>{
  if(x.Country=="Germany"){
    x.Country="Deutschland";
  }
  return x;
});
console.log(responsObj.listMap);

CodePudding user response:

Something like this might help your requirement.

const rawJson = {
    success: true,
    listMap: [
        {
            State: 'Northrine Westfalia',
            Country: 'Germany'
        },
        {
            State: 'Bavaria',
            Country: 'Germany'
        }
    ]
};

for (itemObj of rawJson.listMap) {
    // If needed, you can add check for current value to be 'Germany'
    // if (itemObj.Country === 'Germany')
    itemObj.Country = 'Deutschland';
}

console.log(rawJson);

Output:

{
  success: true,
  listMap: [
    { State: 'Northrine Westfalia', Country: 'Deutschland' },
    { State: 'Bavaria', Country: 'Deutschland' }
  ]
}

CodePudding user response:

Here's a general approach passing in the data, and from, and to arguments. It then uses map to return a new array of objects, changing the country where necessary.

Note: check your data for errors as you had some spare brackets in your example.

const data = {
  success: true,
  listMap: [{
      State: 'Northrine Westfalia',
      Country: 'Germany'
    },
    {
      State: 'Bavaria',
      Country: 'Germany'
    }
  ]
}

function swap(data, from, to) {
  return data.listMap.map(obj => {
    if (obj.Country === from) obj.Country = to;
    return obj;
  });
}

console.log(swap(data, 'Germany', 'Deutchland'));

  • Related