Remove fields from response data

Im new to TYK and I am wondering, is there a way to edit a response data with rich plugins?

Also, is it possible to remove any json which contains a certain key: value?
For example:
Removing all the jsons which contains a “classification”: “high” pair,
So the json:

{
“name”: “apple”,
“lastname”: “banana”,
“classification”: “low”,
“potato”: {
“foo”: “bar”,
“classification”: “high”
},
“raindeer”: {
“foo2”: “bar”,
“classification”: “low”
}
}

Will transform into:

{
“name”: “apple”,
“lastname”: “banana”,
“classification”: “low”,
“raindeer”: {
“foo2”: “bar”,
“classification”: “low”
}
}

Is this possible?

Yes, completely possible with rich plugins.

Though, body transformations are even easier using the body transformation plugin.
https://tyk.io/docs/transform-traffic/request-body/#step-1-create-the-transform

DO you have to use a rich plugin?

edit:

Wait a second, is the JSON always different? or is the classification always found in the same places?

If what you need to do is iteratively loop through all the fields and delete any that are called classification, then yes, you need a rich plugin to write that business logic.

But if you’re statically reshaping a known input/output, the Body Transform plugin is the best solution.

1 Like

Yes, the data structure is unknown, all I need to do is iterate and remove jsons which contains Classification of some kind.

I read about body tranaformations and it doesnt answer my needs because the data structure is unknown.

I started using the python rich plugin but I didn’t know how to access the response data,
I have tried printing the response.raw_body but it seems it always empty, is there another attribute to response I can use to access the response data?

Turns out the body was empty because the middleware was set as “post” instead of response.

1 Like