Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested JSON #372

Closed
richard-churchman opened this issue Jan 15, 2019 · 6 comments
Closed

Nested JSON #372

richard-churchman opened this issue Jan 15, 2019 · 6 comments
Labels
need info Further information has been requested type: question Poses a question, not a problem

Comments

@richard-churchman
Copy link

Hello,

I started with Plumber today and it is working great. When the JSON is flat, it works exactly as expected.

How does Plumber deal with JSON that is nested and multi-layered? How would I craft the function in this manner?

Am I to understand that I have to flatten the structure before i send it over?

Thanks

@shrektan
Copy link
Contributor

I don't understand what you mean. There's no difference between flat JSON and multi-layered JSON, in my opinion. An example may help to understand.

@schloerke schloerke added type: question Poses a question, not a problem need info Further information has been requested labels Feb 22, 2019
@freitasskeeled
Copy link

I was about to ask the same question (I think) so I decided to comment here.

In my case, the application requires that the fromJSON call has the flatten flag set to TRUE. Using Plumber, I'm unable to use the parsed args directly because there is no way to specify that. Alternatively, I use req$postBody with my own parsing.

I checked the code and it seems that there is not way to configure that. safeFromJSON function accepts args that are then passed to jsonlite::fromJSON call but the parseBody function doesn't take advantage of that.

Maybe adding a way to set custom settings for the default filters?

@schloerke
Copy link
Collaborator

Closing due to inactivity

As a note, plumber can parse any valid JSON structure and can also return very complicated JSON structures.

@mrkaye97
Copy link

mrkaye97 commented Aug 30, 2021

@schloerke I just stumbled upon this issue and I think I'm having the same problem. Here's what I want to be able to do:

#* @param bar
#* @post /foo
foo <- function(bar) {
  bar
}

and be able to make requests to that endpoint like this:

POST(
  "<< some url >>/foo"
  body = list(
    baz = list(
      bar = "abc",
      qux = 123
    ) %>% toJSON()
  )
)

but it appears (at least AFAICT) that in order to be able to use nesting like that, I'll need to parse the JSON myself. Is there a best practice for how to deal with nested json like this? I'd almost like to be able to do something like #* @param baz$bar or similar, but I don't know enough about the internals to know if that'd be possible

@meztez
Copy link
Collaborator

meztez commented Aug 30, 2021

Plumber will parse your json input and do a named argument matching only for the top level elements (non nested) via an R do.call.

More detailed

It does a do.call with your handler function foo and args produced by the parser. JSON parser args are top level elements.
do.call :

do.call(private$func, relevant_args, envir = private$envir)

json parser :
parser_json <- function(...) {

safeFromJson method : https://github.com/rstudio/plumber/blob/master/R/json.R

If this is not enough, you could write your own parser as it is relatively easy to extend.

https://www.rplumber.io/reference/register_parser.html

#* @param baz
#* @post /foo
function(baz) {
  baz$bar
}

@mrkaye97
Copy link

mrkaye97 commented Aug 30, 2021

@meztez Aha, gotcha with the updated comment. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need info Further information has been requested type: question Poses a question, not a problem
Projects
None yet
Development

No branches or pull requests

6 participants