0

I have a R data file and inside that file I have data called results_NN3 (it is a type list[111] with a value of list of length 111). I tried to convert results_NN3 to a JSON, to use in python, but I got an error. I am trying to do it this way:

> dados_json <- toJSON(results_NN3)

and the result is:

Error in toJSON(results_NN3) : unable to convert R type 6 to JSON

Sorry if this question is someway wrong, I do not know much R, but I need that file in JSON so I can work with it in python, for a paper. Thanks.

  • Your object is a list of nested lists with all sorts of tpyes in there. (I get an Arima error when I try to run it) Do you need the entire object? or only certain parts of it? – Travasaurus Oct 05 '21 at 17:29
  • 1
    Thanks for your answer @Travasaurus. Actually i just need: results_NN3[["NN3.002"]][["ranked.results"]][["SDIF"]][["pred"]][["pred"]], but for every time series("NN3.001", "NN3.002"...) and every model ("SDIF", "LT", "DIF", "PCT"....) – João David de Freitas Oct 05 '21 at 17:38
  • I think I found that the whole thing can be serialized with a `force = TRUE` argument. Then the subsetting can be done within python. – Travasaurus Oct 05 '21 at 17:51

1 Answers1

1

I had success using the force = TRUE argument:

jsonlite::toJSON(results_NN3, force = TRUE)
{"NN3.001":{"rank":[{"AICc":-69.9076,"AIC":-70.7772,"BIC":-63.0499,"logLik":39.3886,"MSE":419053.9795,"NMSE":1.7235,"MAPE":9.4205,"sMAPE":0.0881,"MaxError":1190.4399,"rank.position.sum":1,"_row":"LT"},{"AICc":-154.9789,"AIC":-155.8485,"BIC":-148.1212,"logLik":81.9242,"MSE":419053.9795,"NMSE":1.7235,"MAPE":9.4205,"sMAPE":0.0881,"MaxError":1190.4399,"rank.position.sum":2,"_row":"LT10"},{"AICc":626.1925,"AIC":625.6344,"BIC":631.1848,"logLik":-309.8172,"MSE":421498.4547,"NMSE":1.7335,"MAPE":9.6515,"sMAPE":0.092,"MaxError":1116.7813,"rank.position.sum":3,"_row":"MAS"},{"AICc":816.5476,"AIC":815.2142,"BIC":824.8734,"logLik":-402.6071,"MSE":463819.2847,"NMSE":1.9076,"MAPE":9.9746,"sMAPE":0.0928,"MaxError":1260.0692,"rank.position.sum":4,"_row":"BCT"},{"AICc":816.5476,"AIC":815.2142,"BIC":824.8734,"logLik":-402.6071,"MSE":463819.2847,"NMSE":1.9076,"MAPE":9.9746,"sMAPE":0.0928,"MaxError":1260.0692,"rank.position.sum":5.5,"_row":"original"},{"AICc":816.5476,"AIC":815.2142,"BIC":...
Travasaurus
  • 601
  • 1
  • 8
  • 26
  • I tried that, but got the erro: Error in toJSON(results_NN3, force = TRUE) : unused argument (force = TRUE) – João David de Freitas Oct 05 '21 at 17:54
  • There may be multiple packages with functions names toJSON in them. Have you tried using jsonlite::toJSON instead of just the function name? – Travasaurus Oct 05 '21 at 18:01
  • I did like that "jsonlite::toJSON(results_NN3, force = TRUE)", but my rstudio shutting down after I execute jsonlite::toJSON(results_NN3, force = TRUE) – João David de Freitas Oct 05 '21 at 18:20
  • Ahh that is a OOM problem. Rstudio tends to shutdown when its overloaded. Try this solution to increase memory allocation https://stackoverflow.com/questions/1395229/increasing-or-decreasing-the-memory-available-to-r-processes#:~:text=Use%20memory.,the%20size%20is%20in%20MB. – Travasaurus Oct 05 '21 at 21:41
  • Thanks a lot for your answers, you helped me a lot. But that method is not getting the part of preds, the predictions of each model for each time series. – João David de Freitas Oct 06 '21 at 02:28
  • can you elaborate a little more? – Travasaurus Oct 06 '21 at 18:38
  • The predictions, they're inside "preds", it's called preds too, the element is a ts(time serie), when i do that method the preds is not on the json file. – João David de Freitas Oct 07 '21 at 00:02