1

I'm trying to get JSON raw response from R Plumber and consume it in Angular, but JavaScript Framework thinks it's a string rather than a JSON format.

"[{\"id\":1,\"type\":\"Diagnostic\"},{\"id\":2,\"type\":\"Impact\"}]"

I saw here and here, but these didn't really help me.

How do I make it so that it's a proper JSON format that JavaScript frameworks can recognize.

#* @apiTitle Diagnostic Report API
#* Send the list of report types
#* @get /reportTypes
#* @serializer unboxedJSON


function(){
  
  reportTypes <- read_csv(file = "ReportTypes.csv")
  # list(
  #   message_echo = paste("The text is:", "text")
  # )
} 

And in the Angular, this is the error that I receive. Given that this is not Angular SO, I just wanted to show the issue I'm running into:

Cannot find a differ supporting object '[{"id":1,"type":"Diagnostic"},{"id":2,"type":"Impact"}]' of type 'string'. NgFor only supports binding to Iterables such as Arrays.
user1828605
  • 1,723
  • 1
  • 24
  • 63
  • 1
    Are you parsing the data on the javascript side? How are you consuming the data? You need to tell javascript to parse the data at some point. All JSON data is encoded as a string during the HTTP request. – MrFlick Jul 24 '20 at 01:45
  • I'm not. I usually never had to do that when I retrieved the data from some database. I'm doing this R + Plumber for the first time, so I'm bit confused. The json format in raw response previously was in a proper format without escapes, and javascript would recognize it that it's an array. As per Plumber's description, the data is already serialized. – user1828605 Jul 24 '20 at 01:50

1 Answers1

1

Not the best thing to do to answer your own question, but I found many questions like this but without a particular solution that worked for me. I felt I should write this as an answer for anyone who'd run into similar situation. Hopefully, it'll save someone hours of painful debugging.

As many of the posts including the ones that I mentioned above correctly assert that plumber automatically serializes the r object. This means, it should be ready for any application requesting the data to consume. But clearly, there were something that I was missing. I had already tried solutions based on these.

Turns out, my express.js server was also parsing the text to json.

router.get('/', function (req, res) {

  request.get({ url: 'http://localhost:5762/reportTypes' },
    function (error, response, body) {
      if (!error && response.statusCode == 200) {
        res.json(body); <-- this is where it's serializing again
      }
    });

});

Credit to all SO community and responders including @MrFlick who discussed about JSON parsing, it gave me a thought perhaps there's somewhere something is serializing the data. So, after digging, it turns out that my express is re-serializing the already serialized data. Simply changing res.json to res.send solved the problem.

Hopefully, this will give some ideas to people who either work on r or javascript and have similar problems, to re-check the codes which is requesting the data again and make sure it's not getting serialized and escaping important characters.

user1828605
  • 1,723
  • 1
  • 24
  • 63