Edgaras Karka
Edgaras Karka

Reputation: 7852

Get json object using R and rjson library

My goal is to implement the response of plumber R HTTP package.

library(rjson)

formatResponse = function(matchId, home, draw, away) {
  return(toJSON(???))
}

formatResponse('myId', 10, 20, 70);

my goal is get:

    {
      matchId: 'myId',
      probabilities: {
        home: 10,
        draw: 20,
        away: 70
      }
    }

Upvotes: 0

Views: 187

Answers (1)

pogibas
pogibas

Reputation: 28379

Straightforward approach: create a list and turn that list into a json object (not the prettiest solution, but works):

formatResponse = function(matchId, home, draw, away) {
    library(rjson)
    foo <- list(matchId = matchId, 
                propabilities = list(home = home, 
                                     draw = draw, 
                                     away = away))
    toJSON(foo)
}
formatResponse("myId", 10, 20, 70)

[1] "{\"matchId\":\"myId\",\"propabilities\":{\"home\":10,\"draw\":20,\"away\":70}}"

Upvotes: 2

Related Questions