Im trying to work with an API, application/json in R but when I use this code:
data <- fromJSON(content(XXX, type = "text"))
I see the next error
Error: lexical error: invalid char in json text.
Producto;IdCompra;VentaN;
(right here) ------^
What can be the problem? I know the data i have to extract is a csv file separated by ";".
Thanks!
I leave the glimpse of my data below
glimpse(data)
List of 10
$ url : chr "https://xxx/x/x/"
$ status_code: int 200
$ headers :List of 16
..$ server : chr "x"
..$ connection : chr "keep-alive"
..$ vary : chr "Origin"
..$ vary : chr "Access-Control-Request-Method"
..$ vary : chr "Access-Control-Request-Headers"
..$ x-content-type-options : chr "nosniff"
..$ x-xss-protection : chr "1; mode=block"
..$ cache-control : chr "no-cache, no-store, max-age=0, must-revalidate"
..$ pragma : chr "no-cache"
..$ expires : chr "0"
..$ strict-transport-security: chr "max-age=31536000 ; includeSubDomains"
..$ x-frame-options : chr "DENY"
..$ content-type : chr "application/json"
..$ content-length : chr "402955"
..$ date : chr "Wed, 10 Nov 2021 20:18:55 GMT"
..$ via : chr "1.1 vegur"
..- attr(*, "class")= chr [1:2] "insensitive" "list"
$ all_headers:List of 1
..$ :List of 3
.. ..$ status : int 200
.. ..$ version: chr "HTTP/1.1"
.. ..$ headers:List of 16
.. .. ..- attr(*, "class")= chr [1:2] "insensitive" "list"
$ cookies :'data.frame': 1 obs. of 7 variables:
..$ domain : chr "#Httpxxxx.com"
..$ flag : logi FALSE
..$ path : chr "/"
..$ secure : logi TRUE
..$ expiration: POSIXct[1:1], format: NA
..$ name : chr "xxxxxx"
..$ value : chr "xxxxxxxxxxxxx"
$ content : raw [1:402955] 43 61 74 65 ...
$ date : POSIXct[1:1], format: "2021-11-10 20:18:55"
$ times : Named num [1:6] 0 0.0621 0.2435 0.6197 0.8178 ...
..- attr(*, "names")= chr [1:6] "redirect" "namelookup" "connect" "pretransfer" ...
$ request :List of 7
..$ method : chr "GET"
..$ url : chr "https://xxxx/x/x/x"
..$ headers : Named chr [1:2] "application/json, text/xml, application/xml, */*" "Bearer xxxxxxxx"| __truncated__
.. ..- attr(*, "names")= chr [1:2] "Accept" "Authorization"
..$ fields : NULL
..$ options :List of 3
.. ..$ useragent: chr "libcurl/7.64.1 r-curl/4.3.2 httr/1.4.2"
.. ..$ cookie : chr "=xxxxxxxxxx"
.. ..$ httpget : logi TRUE
..$ auth_token: NULL
..$ output : list()
.. ..- attr(*, "class")= chr [1:2] "write_memory" "write_function"
..- attr(*, "class")= chr "request"
$ handle :Class 'curl_handle' <externalptr>
- attr(*, "class")= chr "response"
CodePudding user response:
The data you are trying to read is not in json format, so it can't be read with fromJSON
. It appears to be in a semicolon-separated format, so perhaps try
read.table(text = content(XXX, type = "text"), sep = ";", headers = TRUE)