I want to "" in the numbers of my JSON

0

I have a script in node.js that takes json of a site 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 this?

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 })
})  
}
    
asked by anonymous 22.08.2017 / 12:21

1 answer

3

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

So if you want your numbers to be text instead of 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 :

Most elegant way:

x = x.toString();

Shorter 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
    }
}
    
22.08.2017 / 14:26