1

I have a bunch of keys in a JSON file that are defined by numbers like 8374829806766627074.

When I try to read them in R I get completely different numbers. For instance, 8374829806766627074 becomes 8374829806766626816.

I am using the fromJSON in the jsonlite package to read a json file from citrix api.

How can I instead adjust the function to read numbers as characters. I seem to fail finding a solution.

Thanks in advance!

boyansky
  • 11
  • 2
  • 1
    Possible duplicate of [Why are these numbers not equal?](http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal) – jogo Jan 22 '16 at 15:30
  • You have to read characters without converting to numbers. See http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal – jogo Jan 22 '16 at 15:31
  • @jogo That question deals with floating point numbers, which, in my opinion, isn't directly applicable here. I think in this instance, it's more likely to deal with OP's `digits` option and the fact that the number is bouncing up against [the largest number that can be represented in computing](https://en.wikipedia.org/wiki/9223372036854775807). But I do agree that the number should be read in as a character string and not a number. – tblznbits Jan 22 '16 at 15:32
  • 1
    Possible duplicate of [Long Numbers As A Character String](http://stackoverflow.com/questions/32339636/long-numbers-as-a-character-string) – RHertel Jan 22 '16 at 15:35
  • 1
    @jogo Do you know how to force fromJSON to read numbers as characters? Maybe this is the only solution to that. – boyansky Jan 22 '16 at 15:36
  • @brittenbThen eventually the number is not get as longint and because it don't fit to the shorter integer it is handled as double ... . – jogo Jan 22 '16 at 15:37
  • @boyansky Sorry, I have no experience in JSON. One simple method can be put a letter in front of the key: k8374829806766627074 . – jogo Jan 22 '16 at 18:16
  • http://stackoverflow.com/questions/27919403/scientific-notation-issue-in-r the answer from AEBilgrau is interesting, so you can try `x <- 8374829806766627074L` Beside of this the relative different of the two numbers seems to be near the machine-eps. – jogo Jan 27 '16 at 20:16
  • @brittenb http://stackoverflow.com/questions/27919403/scientific-notation-issue-in-r the answer from AEBilgrau is interesting. So the value mentioned in https://en.wikipedia.org/wiki/9223372036854775807 is a theoretical value. The implementation in a progrmming language can differ from the theoretical value. – jogo Jan 27 '16 at 20:29
  • @jogo Thanks for the link. Definitely good information to have. – tblznbits Jan 27 '16 at 20:47
  • Was there ever any resolution to the core question to *"how to force fromJSON ..."*? Appending `L` or prepending some character or manually adding quotes is missing the (clear) point of how to do it _programmatically_. The `jsonlite::fromJSON` function lacks arguments that might help, such as `as.is=TRUE` or `colClasses="character"` (borrowed from other functions for demonstration, clearly they don't work here). – r2evans Jun 21 '22 at 10:42
  • For instance, if external programs are allowed, https://unix.stackexchange.com/a/504448/176511 might be a start, though it does not recurse. – r2evans Jun 21 '22 at 10:47

0 Answers0