How to parse json

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

How to parse json

uris77
I apologize in advance for posting what might be a simple question. I've been trying to figure out how to parse a json request. For now, I've been doing: request.body.text
But I doubt that is the correct way. It returns a  string, which makes it awkward to work with.
Reply | Threaded
Open this post in threaded view
|

Re: How to parse json

uris77
I think I figured it out:
handler {
   context.byContent {
       json {
         def node = parse jsonNode()
         .....
       }
   }
}
Reply | Threaded
Open this post in threaded view
|

Re: How to parse json

uris77
Don't know if this is a bug, but json{} isn't working for me. I had to use type("application/json"):

context.byContent {

                   json {
                        //This doesn't work
                        render json([:])
                    }
                    type("application/json") {
                        render json([:])
                    }
                   
                    html {
                        render "OK"
                    }
                    plainText {
                        render "Plain Text"
                    }
                    type("text/json") {
                        render "TXT"
                    }
                }
Reply | Threaded
Open this post in threaded view
|

Re: How to parse json

Luke Daley
Administrator
In reply to this post by uris77
Here’s a test that shows it being done:


It’s usually as simple as registering the JacksonModule, statically importing ratpack.jackson.Jackson.jsonNode, then…

handler {
  def json = parse(jsonNode())
}

If you want to parse into an object (that Jackson understands)…

handler {
  def thing = parse(fromJson(Thing))
}

Reply | Threaded
Open this post in threaded view
|

Re: How to parse json

uris77
I tried this approach, but I kept getting an error. It complained that it couldn't parse text/plain, it's what lead me to context.byContent. Will try again now that I feel more confident and have a working solution I can always fallback to. Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: How to parse json

Luke Daley
Administrator


This means that your client is broken and is not setting the Content-Type header to be application/json.

Do you own the client?
Reply | Threaded
Open this post in threaded view
|

Re: How to parse json

uris77
Yeah, I tried with Postman, then with nodejs.
Reply | Threaded
Open this post in threaded view
|

Re: How to parse json

Luke Daley
Administrator
So do you have any explanation why these clients are sending JSON, but with a plain text content type?
Reply | Threaded
Open this post in threaded view
|

Re: How to parse json

uris77
Will create a sample project and possibly a screencast later tonight. They are not sending plain text, if I inspect the header in the handler, I can see its application/json. Anyway, will create a sample app and try to reproduce.
Reply | Threaded
Open this post in threaded view
|

Re: How to parse json

Luke Daley
Administrator
Cool, a bug in Ratpack is easier for me to fix than a bug in node :)