In addition to strings and numbers, valid JSON can contain special values like null
and false
I need to parse a JSON generated by some API that also contains undefined
. However, undefined
is a valid JavaScript value, but it is not a valid JSON value, and whenever I parse it it returns a lexical error.
Examples:
library(jsonlite)
# A string works
"[{\"Sepal.Width\":\"3.5\"}]" %>% fromJSON
# Sepal.Width
# 3.5
# A number works
"[{\"Sepal.Width\":3.5}]" %>% fromJSON
# Sepal.Width
# 3.5
# null works
"[{\"Sepal.Width\": null}]" %>% fromJSON
# Sepal.Width
# NA
# false works
"[{\"Sepal.Width\": false}]" %>% fromJSON
# Sepal.Width
# FALSE
# undefined does not work
"[{\"Sepal.Width\": undefined}]" %>% fromJSON
Error: lexical error: invalid char in json text.
[{"Sepal.Width": undefined}]
(right here) ------^
Question:
Is there any (reliable) way to parse JSON containing undefined
values? If not, what is the best approach to repair this faulty JSON?
Attempt:
I've thought about simply gsubbing undefined
, but that is risky, since that word could easily exist in the JSON string values.