Blog
  • AI Gateway
  • AI Security
  • AIOps
  • API Security
  • API Gateway
    • API Management
    • API Development
    • API Design
    • Automation
    • Service Mesh
    • Insomnia
    • View All Blogs
  1. Home
  2. Blog
  3. Engineering
  4. 4 Ways to Leverage Kong’s jq Plugin
Engineering
December 7, 2021
6 min read

4 Ways to Leverage Kong’s jq Plugin

Michael Heap
Sr Director Developer Experience, Kong
Topics
Kong GatewayPluginsAPI Development
Share on Social

More on this topic

eBooks

Maturity Model for API Management

eBooks

API Infrastructure: ESB versus API Gateway

See Kong in action

Accelerate deployments, reduce vulnerabilities, and gain real-time visibility. 

Get a Demo

As part of the Kong Gateway 2.6 release, we shipped a brand new jq plugin for anyone with an enterprise license to use. It’s like we combined the request and response transformer plugins to form a single, more powerful plugin—supercharging the way we work with request and response bodies.

If you're not familiar with jq, it's a JSON processing language that allows you to manipulate any JSON document and transform it however you need. We won't cover everything that jq can do in this post, but if you'd like a step-by-step introduction, I recommend this post on earthly.dev.

The Kong Gateway jq plugin allows you to run your transforms conditionally, depending on the request or response media type (it defaults to application/json) and response code. This means that you won't try to transform a response body if you get an error response by default.

To configure your jq filter, you need to provide the request_jq_program or response_jq_program options in your plugin config, or you can specify both to transform both the request and the response.

The Kong documentation has a comprehensive overview of the available options, which can be provided to the admin API as an HTTP request or via declarative configuration. Here's a sample declarative configuration file that uses jq to extract the title key from multiple items in a response:

Or using the admin API to configure the plugin:

Example Use Cases

The best way to learn jq is to see it used in some real-world scenarios. In this post, we'll cover three real-world scenarios and one fun example to show the power of jq:

  1. Transform a request body from one structure to another
  2. Remove sensitive information from a response
  3. Convert a JSON API to CSV
  4. Convert Celsius to Fahrenheit (this is the fun one!)

Let's dive in with how to restructure a request body before sending it to the upstream.

1. Transform a Request Body to Maintain Backwards Compatibility

Imagine that we've shipped an API to a handful of early access customers, and they've integrated it and are using the API in production. Then disaster strikes—we realize that we've made a typo in one of the field names! We accidentally named a field referer rather than referrer.

How could we fix this without impacting our early access customers? jq to the rescue!

By assigning a field using = and the del() function, we can create a new field containing the value of referer and delete the old key. This has the effect of renaming a key in the request body:

Although this program contains spaces, there is no need to encode the request:

2. Delete Sensitive Information From a Response

The next scenario is a backend team built an API for us, but they've left in some sensitive information that we don't want to share with customers. We asked for it to be removed, but the API should have been launched last week, and we can't wait another two weeks for the team to make the changes.

We want to launch today but only show the standard pricing field:

How do we remove these sensitive fields from the response dynamically? Once again,jq to the rescue! There are a couple of ways to achieve what we want to do. The first is to call del() multiple times:

This works but isn't the most efficient way to accomplish our goal. Instead, we can step in to pricing then delete multiple keys within that object:

Once we apply this transform using response_jq_program, we'll get the following response body:

The data we wanted to remove is gone, and we can ship our new API today!

3. Convert a JSON Response to CSV

Imagine that we built a lovely JSON API for our customers that works great, but then one of our valuable customers comes back to us saying, "But it doesn't work in my spreadsheets! Can you provide the data as CSV too?"

We don't want to invest more development time for just one customer, but we want to make them happy. How do we tackle it? That's right! We can use jq.

In this example, we'll be returning a list of sessions presented at Kong Summit 2021 through an API. Here's a sample of what the API looks like:

This is a slightly more complicated processing pipeline than the examples shown so far, so we'll build it up section by section. The first row we need to output is a list of field names. We can do this by fetching the keys from the first entry in the array:

Next, we need to process the payload to remove any complex data structures as they can't be represented in a CSV. In our API, the only complex data is the list of presenters, which we'll convert to a string by mapping over each entry and joining the list with a comma:

Now that the data only contains strings, we need to remove all key names. We can do that using map and $keys. It looks odd, but what we're saying is "add a row containing all of the available keys, then return a list of values, one for each available key":

At this point, our data looks like the following:

The final thing to do is to remove the wrapper array and convert it to CSV:

Putting it all together, we get the following:

Which returns a CSV response:

We're almost there, but not quite! We don't want to return a CSV payload to everyone, so we need to register a new route using Kong. We expect consumers to send an Accept: text/csv header to get the CSV response. Here's the configuration I used to create this route:

update-route

Finally, we have to enable the jq plugin on our new route. We can use the jq expression above as config.request_jq_program like normal, but we need a few other configuration options when working with CSV. The default options for jq are to compress the output and escape any quotes. As we're returning CSV data, we don't want this to happen, so we need to set config.response_jq_program_options.compact_output to false and config.response_jq_program_options.raw_output to true.

Once we set those configuration options, we're all good to go! We now have an endpoint that returns CSV data for our high-profile customer with zero development effort.

4. Convert Celsius to Fahrenheit

One final example before you go and try the jq plugin out yourself.

Imagine this—we've got a wildly successful greenhouse temperature monitoring product that's working really well in the EU, and we want to launch the product in the US. We're unsure if it will do as well, so we do some initial user testing. Feedback is positive except for one thing—they'd prefer to see the temperature in Fahrenheit rather than Celsius.

As it's a small batch of testers that we're very hands on with, we decide to convert on the fly using jq to validate our hypothesis rather than getting our engineering team to build the functionality. An API that returns data in the following format powers our application:

Our UI uses the units and temperature from the API, so we need to rewrite those two fields in both current and previous to be in Fahrenheit. Once again, we'll be using map to apply to each entry in the response.

This will convert the temperature and units values in every key in the returned object. This works for the simple use case above, but what if our response was a little more complex? What if it also contained a location and an account ID?

Using the jq expression above would result in an error:

Thankfully, jq also supports if statements that allow us to run our transform conditionally. Here's a transformation that checks that the units key has a value of celsius before trying to convert to Fahrenheit. If it doesn't equal celsius then it returns the original object:

Once we apply this jq plugin to a specific route that we're using in our beta testing, we can go ahead and test with US customers once again - this time with the units they're familiar with.

Give It a Go!

That's all we've got time for today, but hopefully, it's given you an idea of how the jq plugin works. You can learn about the available plugin options on Kong's plugin hub or learn more about jq itself by reading the jq manual.

We'd love to hear more about what you're doing with jq and Kong. You can tweet us at @thekonginc, and who knows, maybe we'll get together and do a jq themed episode of Kong Builders.

Developer agility meets compliance and security. Discover how Kong can help you become an API-first company.

Get a DemoStart for Free
Topics
Kong GatewayPluginsAPI Development
Share on Social
Michael Heap
Sr Director Developer Experience, Kong

Recommended posts

Unlocking API Analytics for Product Managers

Kong Logo
EngineeringSeptember 9, 2025

Meet Emily. She’s an API product manager at ACME, Inc., an ecommerce company that runs on dozens of APIs. One morning, her team lead asks a simple question: “Who’s our top API consumer, and which of your APIs are causing the most issues right now?”

Christian Heidenreich

Announcing terraform-provider-konnect v3

Kong Logo
Product ReleasesAugust 22, 2025

It’s been almost a year since we released our  Konnect Terraform provider . In that time we’ve seen over 300,000 installs, have 1.7 times as many resources available, and have expanded the provider to include data sources to enable federated managem

Michael Heap

How to Build a Multi-LLM AI Agent with Kong AI Gateway and LangGraph

Kong Logo
EngineeringJuly 31, 2025

In the last two parts of this series, we discussed How to Strengthen a ReAct AI Agent with Kong AI Gateway and How to Build a Single-LLM AI Agent with Kong AI Gateway and LangGraph . In this third and final part, we're going to evolve the AI Agen

Claudio Acquaviva

How to Build a Single LLM AI Agent with Kong AI Gateway and LangGraph

Kong Logo
EngineeringJuly 24, 2025

In my previous post, we discussed how we can implement a basic AI Agent with Kong AI Gateway. In part two of this series, we're going to review LangGraph fundamentals, rewrite the AI Agent and explore how Kong AI Gateway can be used to protect an LLM

Claudio Acquaviva

How to Strengthen a ReAct AI Agent with Kong AI Gateway

Kong Logo
EngineeringJuly 15, 2025

This is part one of a series exploring how Kong AI Gateway can be used in an AI Agent development with LangGraph. The series comprises three parts: Basic ReAct AI Agent with Kong AI Gateway Single LLM ReAct AI Agent with Kong AI Gateway and LangGr

Claudio Acquaviva

Build Your Own Internal RAG Agent with Kong AI Gateway

Kong Logo
EngineeringJuly 9, 2025

What Is RAG, and Why Should You Use It? RAG (Retrieval-Augmented Generation) is not a new concept in AI, and unsurprisingly, when talking to companies, everyone seems to have their own interpretation of how to implement it. So, let’s start with a r

Antoine Jacquemin

AI Gateway Benchmark: Kong AI Gateway, Portkey, and LiteLLM

Kong Logo
EngineeringJuly 7, 2025

In February 2024, Kong became the first API platform to launch a dedicated AI gateway, designed to bring production-grade performance, observability, and policy enforcement to GenAI workloads. At its core, Kong’s AI Gateway provides a universal API

Claudio Acquaviva

Ready to see Kong in action?

Get a personalized walkthrough of Kong's platform tailored to your architecture, use cases, and scale requirements.

Get a Demo
Powering the API world

Increase developer productivity, security, and performance at scale with the unified platform for API management, AI gateways, service mesh, and ingress controller.

Sign up for Kong newsletter

Platform
Kong KonnectKong GatewayKong AI GatewayKong InsomniaDeveloper PortalGateway ManagerCloud GatewayGet a Demo
Explore More
Open Banking API SolutionsAPI Governance SolutionsIstio API Gateway IntegrationKubernetes API ManagementAPI Gateway: Build vs BuyKong vs PostmanKong vs MuleSoftKong vs Apigee
Documentation
Kong Konnect DocsKong Gateway DocsKong Mesh DocsKong AI GatewayKong Insomnia DocsKong Plugin Hub
Open Source
Kong GatewayKumaInsomniaKong Community
Company
About KongCustomersCareersPressEventsContactPricing
  • Terms•
  • Privacy•
  • Trust and Compliance•
  • © Kong Inc. 2025