Can JSON numbers be quoted?

JsonStringParsingNumbersQuotes

Json Problem Overview


Can there be quotes around JSON numbers? In most of the search links, it seems that numbers do not "require" quotes. But, should the parsers accept both "attr" : 6 and "attr" : "6"?

If MyParser has a method getInt to get a number given the key, should MyParser.getInt("attr") return 6 in both cases, or throw an exception in the latter case?

Json Solutions


Solution 1 - Json

In JSON, 6 is the number six. "6" is a string containing the digit 6. So the answer to the question "Can json numbers be quoted?" is basically "no," because if you put them in quotes, they're not numbers anymore.

> But, should the parsers accept both "attr" : 6 and "attr" : "6"?

Yes, but they define different things. The first defines an attr property with the value 6 (a number). The second defines an attr property with the value "6" (a string containing a single digit).

(The question originally asked about attr: "6", which is invalid because property names must be in double quotes in JSON.)

> If MyParser has a method getInt to get a number given the key, should MyParser.getInt("attr") return 6 in both the cases or throw an exception in the latter case?

That's a design decision for the person providing the parser, basically the choice between getInt being strict (throwing an exception if you try it on "attr": "6") or loose (coercing "6" to 6 and returning that). JavaScript is usually loose, and so there could be an argument for being loose; conversely, the fact that JavaScript is loose sometimes causes trouble, which could be an argument for being strict.

Solution 2 - Json

That will depend of the language that you use to get the integer, because if the programming language does not provide implicit conversion from string to int, you can have problems.

You should not to worry too much, since modern programming language nowadays can implicitly convert a string to a number without additional code. Something you should take into consideration is, when using programming languages like JavaScript, when you use == and === when comparing values, === takes into consideration the value's type while == not, so 6 === "6" will return false, while 6 == "6" will return true.

Answering your question, it will not throw an exception if you are using a programming language that provides implicit conversion from string to int.

Solution 3 - Json

You can quote the number. Both are valid ("attr" : 6 and "attr" : "6") The only thing need to keep in mind that while extracting the value you need to use GetInt() in first case and GetString() in second case and then convert that string to integer.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUnreal userView Question on Stackoverflow
Solution 1 - JsonT.J. CrowderView Answer on Stackoverflow
Solution 2 - JsonscubaFunView Answer on Stackoverflow
Solution 3 - Jsonuser6882413View Answer on Stackoverflow