How to get only key and value from stringifying json after API fetch?

Tags: , , , ,



I’m trying to display only the key and value on an EJS page.

<span><%= JSON.stringify(results.conversion_rates) %></span>

leaves me with

{"USD":1,"AED":3.6721,"ARS":81.0455,"AUD":1.3564,"BGN":1.6324}

How could I remove the brackets and quotations?

Here is my server.js route if that helps:

app.get('/results', (req, res) => {
  const query = req.query.q;
  axios
    .get(`https://v6.exchangerate-api.com/v6/a66b8aae93f6e7abafe3aab5/latest/${query}`)
    .then(function (response) {
      const currencyPair = `Conversion Rates for ${query}`;
      console.log(response)
      res.render('results', {
        currencyPair,
        results: response.data,
      });
    })
});

Answer

Maybe you need just to remove all quotes and single quotes? For this reason, You don’t need to iterate through Object.

let incomeJson = { "USD":1,"AED":3.6721,"ARS":81.0455,"AUD":1.3564,"BGN":1.6324 };

console.log( JSON.stringify(incomeJson).replace(/["']/g, ""));


Source: stackoverflow