KB Article #177149

A selector to return a field value from an application/json body

Problem

How to write a selector that returns a string field value from an application/json body.

e.g. with the following JSON content in the incoming request

{
"access_token":"2YotnFZFEj",
"token_type":"example",
"expires_in":3600
}

and it is required to set an attribute access.token with the value 2YotnFZFEj

Resolution

If a body is of type application/json then it is automatically treated as a com.vordel.mime.JSONBody. A JSONBody object returns a org.codehaus.jackson.JsonNode object via a getJSON() call.

Use a Set Attribute Filter with the selector below :-

Name:Set Access Token Attribute
Attribute Name:access.token
Attribute Value:${content.body.getJSON().get("access_token").getTextValue()}