I'm trying to use the fromJSON function in R to read in a JSON file that was given to me, however this file has NaN
in it and I can't read it in properly.
This is the error I get:
Error in feed_push_parser(buf) :
lexical error: invalid char in json text.
Anyone know how to read NaN
values when reading in a json file into R?
The fromJSON() function reads the content in JSON format and de-serializes it into R objects. JSON content is produced of logical values, integers, real numbers, strings, arrays using the key: value pairs. The toJSON and fromJSON methods use class-based mapping.
jsonlite: A Simple and Robust JSON Parser and Generator for R. A reasonably fast JSON parser and generator, optimized for statistical data and the web. Offers simple, flexible tools for working with JSON in R, and is particularly powerful for building pipelines and interacting with a web API.
As referenced in the comments, RJSONIO can handle NaN. By default, the NaN values will be excluded. If you want to include the NaN values you can set the NaN values to NA via nullValue.
Example Code - Replacing NaN with NA
library(RJSONIO)
json_imported <- fromJSON(content,nullValue=NA)
The value for 'content 'is the JSON content. Via RJSONIO documentation, "This can be the name of a file or the content itself as a character string. We will add support for connections in the near future."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With