Rajesh Kumar Duraisamy
Rajesh Kumar Duraisamy

Reputation: 797

How to return JSON using plumber api in R

I am new to R . I am supposed to expose rest service using R , So i found plumber to expose rest service using R , I Successfully implemented plumber in R , but in response i am receiving a json array like below

[{"name":"Rajesh","age":"10"}]

how to remove the array from the above response

my expected output is like below

{"name":"Rajesh","age":"10"}

Code

 library(plumber)
r <- plumb("MyFile.R")
r$run(port=8000)

My File .R is mentioned below

    #* @post /sum
addTwo <- function(){
  name<-c("Rajesh")
  age<-c("10")
  df<-data.frame(name,age)

  return(df)

}

Upvotes: 8

Views: 3343

Answers (1)

Eoghan Hynes
Eoghan Hynes

Reputation: 41

library(jsonlite)

df <- jsonlite::toJSON(data.frame(name, age, auto_unbox=TRUE))

Upvotes: 1

Related Questions