How to convert float values in JSON to two decimal place using Javascript

Viewed 7196

I have a JSON file that contains numbers of type float. I would like to convert them to two decimal places before using the file in a react application. I understand that I need to use number.toFixed(2) to achieve that. How could I achieve that in the JSON file? A sample of the file:

[
{
    "module": "prado2",
    "current_value": 3437.0,
    "value_avg": 3436.5,
    "value_avg_plus_stdv": 3457.7132034355964,
    "value_avg_minus_stdv": 3415.2867965644036,
},
{
    "module": "prado2",
    "current_value": 3437.0,
    "value_avg": 3436.5,
    "value_avg_plus_stdv": 4457.7132034355964,
    "value_avg_minus_stdv": 3425.2867965644036,
}
]

The final result should be :

[
{
    "module": "prado2",
    "current_value": 3437.0,
    "value_avg": 3436.5,
    "value_avg_plus_stdv": 3457.71,
    "value_avg_minus_stdv": 3415.29,
},
{
    "module": "prado2",
    "current_value": 3437.0,
    "value_avg": 3436.5,
    "value_avg_plus_stdv": 4457.71,
    "value_avg_minus_stdv": 3425.29,
}
]

Thanks!

5 Answers

You could pass reviver function to the JSON.parse method to prescribes how the value originally produced by parsing is transformed, before being returned:

JSON.parse(jsonString, (key, value) =>
  typeof value === "number" ? Math.round(value * 100) / 100 : value
);

Replace Math.round(value * 100) / 100 with Math.round((value + Number.EPSILON) * 100) / 100 or +value.toFixed(2) or other methods as you wish.

More about JSON.parse

use toFixed

Like this

[
{
    "module": "prado2",
    "current_value": 3437.0,
    "value_avg": 3436.5,
    "value_avg_plus_stdv": 3457.7132034355964,
    "value_avg_minus_stdv": 3415.2867965644036,
},
{
    "module": "prado2",
    "current_value": 3437.0,
    "value_avg": 3436.5,
    "value_avg_plus_stdv": 4457.7132034355964,
    "value_avg_minus_stdv": 3425.2867965644036,
}
].map(x => ({
    ...x,
    value_avg_plus_stdv: +x.value_avg_plus_stdv.toFixed(2),
    value_avg_minus_stdv: +x.value_avg_minus_stdv.toFixed(2)
}))

Edit - toFixed will return a string, so add a + to convert it to number

You will need to loop the array of objects, and apply the function you mentioned on each number property.

First you will need to parse that JSON to get the JavaScript Object, and then apply something like this snippet:

const newArray = yourArray.map((obj) => ({
      ...obj,
      value_avg_plus_stdv: +obj.value_avg_plus_stdv.toFixed(2),
      value_avg_minus_stdv: +obj.value_avg_minus_stdv.toFixed(2),
    }));

The + on the toFixed() will convert the strings to numbers

Let's go through your problem step-by-step

1) Get the file

In the further steps I will assume that you have already got the file and read its content into a String, I will call it rawInput from now on.

2) Convert your String into an object

var rawInput = `[
{
    "module": "prado2",
    "current_value": 3437.0,
    "value_avg": 3436.5,
    "value_avg_plus_stdv": 3457.7132034355964,
    "value_avg_minus_stdv": 3415.2867965644036
},
{
    "module": "prado2",
    "current_value": 3437.0,
    "value_avg": 3436.5,
    "value_avg_plus_stdv": 4457.7132034355964,
    "value_avg_minus_stdv": 3425.2867965644036
}
]`;
var input = JSON.parse(rawInput);

Now fix your object's items:

input

3) Make the conversion for numeric fields:

for (let item of input)
    for (let att in item)
        if (typeof item[att] === "number") item[att] = item[att].toFixed(2);

4) Convert it back to String:

var output = JSON.stringify(input)

5) Output

"[{\"module\":\"prado2\",\"current_value\":\"3437.00\",\"value_avg\":\"3436.50\",\"value_avg_plus_stdv\":\"3457.71\",\"value_avg_minus_stdv\":\"3415.29\"},{\"module\":\"prado2\",\"current_value\":\"3437.00\",\"value_avg\":\"3436.50\",\"value_avg_plus_stdv\":\"4457.71\",\"value_avg_minus_stdv\":\"3425.29\"}]"

If you just need to truncate the numbers to a specified number of digits here is a command line tool that does that.

https://pypi.org/project/jsonvice/

Its written in python but has the logic in it. You can also choose whether to round, truncate or floor the numbers.

disclaimer I'm the author, but I ran in to this issue when compressing large machine learning models.

Related