从 json 请求 body 获取值并存储在 Azure API 管理中的变量中

Fetch value from json request body and store in a variable in Azure API Management

我想从 json 中获取一个值并将其存储在 Azure API 管理中的一个变量中。 JSON请求Body中的例子是

{
    "ItemCode": 1,
    "ItemName": "USA",
    "typeCode": "REG"
  }

我需要获取 ItemCode 和 typeCode 的值并将其存储在变量中。

我查看了 Microsoft Docs,它让我可以通过使用液体模板来转换 body,我认为这对我的要求没有用。

我已经将 JSON 存储在一个变量中,例如

set-variable name="varItemCode" value="@(context.Request.Body.As<String>(preserveContent:true))" />

因为这是存储的字符串,所以我无法遍历 JSON Object.

我做到了

<kbd>set-variable name="varTypeCode" value="@{
                    JObject json = JObject.Parse(context.Variables.GetValueOrDefault<string>("varBody"));
                    var typeCode = json.GetValue("typeCode");
                    return typeCode;
                 }" />

这是有效的

 <set-variable name="validationResults" value="@(context.Request.Body.As<JObject>())" />
    
<set-variable name="operation" value="@((string)((JObject)context.Variables["validationResults"])["operation"])" />