Home About Me

When Query Strings Need to Become JSON: A Small Kong Plugin

query-string-to-json is a Kong plugin for one specific transformation: it takes URL query parameters and moves them into a JSON string stored under a query parameter, then rewrites the original URL accordingly. The project is at https://github.com/qianyugang/kong-query-string-to-json.

By default, the JSON lives under params, but that key is configurable. The plugin also accepts a list of parameter names to leave untouched, which is useful when some query values need to remain top-level and only the rest should be bundled into JSON.

The effect is easiest to see with an example. Given this URL:

http://service-host.com?minitest?appkey=xxx&size=10&qq=11&ww=22

After the request passes through Kong with the plugin enabled, it becomes:

http://kong.com:8000?appkey=xxx&size=10&params={"qq":"11","ww":"22"}

Here appkey and size stay as ordinary query parameters because they are excluded. The qq and ww values are grouped into the params JSON object.

Configuration uses two settings:

  • exclude_params_names: parameter names that should not be converted into JSON.
  • params_name: the query parameter key that will hold the resulting JSON; defaults to params.

After a service and route have been configured for proxying, the plugin can be attached to the service or route that needs it. For example:

curl -X POST http://192.168.1.5:8001/routes/1ed4443c-e677-4af4-b551-ad6fd99a55e1/plugins\ --data "name=kong-query-string-to-json" \ --data "config.exclude_params_names[]=size&config.exclude_params_names[]=appkey"

Compatibility: Kong 2.0.0 and later.

kong >= 2.0.0