I want to put "" in my JSON numbers

Asked

Viewed 350 times

0

I have a script in node.js who catches the json from a website that comes in this format:

{"nome123":123.4,"nome213":231.4."nome123":123.4}

I want to quote the numbers and leave it like this:

{"nome123":"123.4","nome213":"231.4","nome123":"123.4"}

How do I do that?

const saPrices = `https://api.csgofast.com/sih/all`
Trade.prototype.getSteamapis = function getSteamapis(callback) {
  request(saPrices, (error, response, body) => {
    if (!error && response.statusCode === 200) {
        const items = JSON.parse(body)
        return callback(null, items)
    }
    const statusCode = (response) ? response.statusCode || false : false
    return callback({ error, statusCode })
})  
}
  • 1

    Hello Luciandro, Welcome to Stackovervflow pt. Try to increase your question more with a piece of code, to help the community better understand your question

  • You really need to change those numbers to strings?

  • All values of this object are numbers? or are there other values of text, arrays, etc?

1 answer

3

In a JSON object, if a value is in quotes, this is an indication that it is a string. Numbers themselves are not enclosed in quotes.

So, if what you want is for your numbers to be even text and not quotes, you can turn them into text in your code.

To transform a number into text in Javascript, there are two ways. Assuming a variable any call x:

More elegant shape:

x = x.toString();

Shortest form:

x = x + "";

Finally, you can scan your JSON object like this:

for (var x in json) {
    if (typeof json[x] === "number") {
        json[x] = json[x] + ""; // ou pode usar a forma elegante
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.