Access the JSON property from JSON Response - Azure APIM Policies

Vijay Sharma 0 Reputation points
2024-04-08T15:35:31.67+00:00

Hi,

If I am getting below JSON response from an API, how can I get the status and code properties in that.

{
  "error": {
    "status": 0,
    "code": "string",
    "message": "string",
    "target": "string",
    "instance": "string",
    "details": [
      "string"
    ]
  }
}

Below is my policy code which is currently in place.

<policies>

<inbound>

    <base />

</inbound>

<backend>

    <base />

</backend>

<outbound>

	<base />

	<set-variable name="errorResponse" value="@(context.Response.Body.As<JObject>()["error"])" />

	<choose>

		<when condition="@(context.Variables["errorResponse"]!=null)">

			<set-variable name="errorStatus" value="@{

				var jsonObject = JObject.Parse(context.Response.Body.As<JObject>()["error"]);

				var value = (string)jsonObject["status"];

				return value;

			}" />

			<set-variable name="errorStatus" value="@{

				var jsonObject = JObject.Parse(context.Response.Body.As<JObject>()["error"]);

				var value = (string)jsonObject["code"];

				return value;

			}" />

			<choose>

				<when condition="@((int)context.Variables["errorStatus"]==404)">

					<return-response>

						<set-status code="200" reason="NotFound" />

						<set-body template="liquid">

							{ 

								"status": "NotFound"

							}

						</set-body>

					</return-response>

				</when>

			</choose>

		</when>

	</choose>

</outbound>

<on-error>

    <base />

</on-error>

</policies>

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,446 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Aki Nishikawa 2,455 Reputation points Microsoft Employee
    2024-04-17T05:40:10.56+00:00

    If the specified JSON is returned from backend service, you can retrieve the code and status in the following way. This is a simple example, and few error handling is implemented. You should care about this.

    The following points should be considered.

    [Response from backend service]

    {
      "error": {
        "status": 0,
        "code": "string",
        "message": "string",
        "target": "string",
        "instance": "string",
        "details": [
          "string"
        ]
      }
    }
    

    [policy]

    ...
        <outbound>
            <base />
            <return-response>
                <set-status code="200" />
                <set-body>@{
                    var body = context.Response.Body.As<JObject>(true);
                    if(body["error"]!= null && body["error"].HasValues ) {
                        return new JObject(
                            new JProperty("code", (string)body["error"]["code"]),
                            new JProperty("status", (int)body["error"]["status"])
                        ).ToString();
                    }
                }</set-body>
            </return-response>
        </outbound>
    ...
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.