Java transform JSON to application/x-www-form-urlencoded

Viewed 1129

As an input I have a JSON with the following structure:

{
  "amount": 2000,
  "currency": "usd",
  "description": "test charge"
}

I need to send a request where this JSON is in application/x-www-form-urlencoded content type. So the data should be in this format amount=2000&currency=usd&description=testcharge.

My following logic would look like :

  1. Parse the JSON to Map<String, String> with Gson/Jackson
  2. Create a generic method to build key value string

My first question : Is there any Java library used for converting a JSON to application/x-www-form-urlencoded? Or isn't there any library for building key value url encoded pairs?

This wouldn't be a big a deal also without library but

My Second question : How will look the application/x-www-form-urlencoded query if given json will contain some nested objects/arrays?

{
  "amount": 2000,
  "currency": "usd",
  "shipping": {
    "address": {
      "city": "Seattle",
      }
   }
}

Would look like? amount=2000&currency=usd&shipping=???.

That's why I would like to use rather library than my own parsing..

0 Answers
Related