4

What is best way to parse JSON number as String in javascript

Example:
{
  "a": 10.00
}

Notice I do not have control over value 10.00. I cannot add there "+''". I want to keep the decimal places, but it is not rule that there have to be 2 decimals.

Result should be 10.00 not 10

12
  • 2
    JSON.parse will do just fine. The actual number representation for 10 and 10.00 are exactly the same. If you want to display/format the number in a certain way then you can use .toFixed: (10)..toFixed(2). If you want to display the number exactly how it is written in the JSON document then you are out of luck. The value would have to be provided as a string. Commented Mar 25, 2020 at 20:29
  • You need "a" to be the string "10.00" ? Commented Mar 25, 2020 at 20:30
  • 1
    Looking at your question: no you don't. You want to show that value to users at some point, with 2 decimals after the decimal point. So do that instead, as per Felix's comment. Use JSON.parse, which will make it the number 10, and then format it only at the time it needs to be displayed. Commented Mar 25, 2020 at 20:37
  • 1
    yes, and you do that using .toFixed(2), you don't change the input at all, you just use <p class="amount">${{ parsed.a.toFixed(2) }}</p> or whatever your render engine's equivalent is. Commented Mar 25, 2020 at 20:38
  • 1
    then you're going to have to live with the fact that Javacript cannot see the difference between 10 and 10.00: they're literally the same value at runtime. If you need it to be strings, then either send your json with quotes around the value (e.g. "10.00" not 10.00, "1.928346", not 1.928346, etc) but if you can't do that, then you are out of luck: write code that checks whether there is a fractional value, if there is, use the number directly, and if there isn't, use toFixed(...) if you always want to see a minimum number of decimal places. Commented Mar 25, 2020 at 20:43

3 Answers 3

9

If you need exactly the same number of decimals, the only way is if the JSON present the value as string. If you have no control over the source, you could edit the JSON before parsing, adding the quotes, but that could bring several problems. This needs to be tested toroughly.

json = '{ "a": 10.00, "b":2.1020, "d":0.20,"c": "21" }';

json = json.replace(/:\s*[^"0-9.]*([0-9.]+)/g, ':"$1"');

console.log(json);
console.log(JSON.parse(json));

Sign up to request clarification or add additional context in comments.

1 Comment

Just to emphasise, that This needs to be tested toroughly notice is very important to pay attention to, and if you use this approach, remember to add tests because it can just as easily completely break other parts of your code.
1

You can use JSON.parse with a reviver argument

const json = '{"a":{"b":0.1}}'

const data = JSON.parse(json, (_key, value, data) => typeof value === 'number' ? data.source : value)

console.log(data);

Comments

0

You can use regular expressions to extract the value of a property, before JSON string is parsed and string 10.00 is converted to the number 10:

const jsonString = `{
  "a": 10.00
}`
const aString = jsonString.match(/[,{]\s*\"a\":\s*([0-9.]+)/m)[1]
console.log(aString)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.