【Azure Developer】Azure Logic App 示例: 解析 Request Body 的 JSON 的表达式? triggerBody()?

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 【Azure Developer】Azure Logic App 示例: 解析 Request Body 的 JSON 的表达式? triggerBody()?

问题描述

通过Azure Logic App(逻辑应用)实现无代码的处理JSON数据。但是如何获取Request Body中的一个属性值呢? 例如:如何来获取以下JSON结构中的 ObjectName 值?

Request对象中所含的Body内容(黄色高亮部分):

{
    "headers": {
        "Connection": "close",
        "Accept": "*/*",
        "Accept-Encoding": "br,gzip,deflate",
        "Accept-Language": "en",
        "Host": "prod-24.chinaeast2.logic.azure.cn",
        "User-Agent": "Mozilla/5.0,(Windows NT 10.0; Win64; x64),AppleWebKit/537.36,(KHTML, like Gecko),Chrome/97.0.4692.99,Safari/537.36,Edg/97.0.1072.69",
        "x-ms-client-session-id": "22ea27e05a1b4637b2aa8f2b3a7a3039",
        "x-ms-command-name": "RunWithPayloadConsumptionBlade.runTrigger",
        "x-ms-effective-locale": "en.en-us",
        "x-ms-client-request-id": "b73db2d0-ec46-436d-bfef-a2d2b7e9a00d",
        "origin": "https://portal.azure.cn",
        "sec-fetch-site": "same-site",
        "sec-fetch-mode": "cors",
        "sec-fetch-dest": "empty",
        "Content-Length": "913",
        "Content-Type": "application/json"
    },
    "body": [
        {
            "id": "4be730e8-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            "topic": "/subscriptions/xxxx-xxxx-xxxx-xxxx-xxxx/...",
            "subject": "sqlPassword",
            "eventType": "Microsoft.KeyVault.SecretNearExpiry",
            "data": {
                "Id": "......",
                "VaultName": "xxxx-kv",
                "ObjectType": "Secret",
                "ObjectName": "sqlPassword",
                "Version": "ee550dxxxx",
                "NBF": null,
                "EXP": 1643111997
            },
            "dataVersion": "1",
            "metadataVersion": "1",
            "eventTime": "2022-01-25T11:54:50.6358008Z"
        }
    ]
}

如果直接在Logic App使用triggerBody()?['ObjectName'] 则遇见如下错误:

InvalidTemplate. Unable to process template language expressions in action 'Send_message' inputs at line '0' and column '0': 'The template language expression 'base64(triggerBody()?['objectName'])' cannot be evaluated because property 'objectName' cannot be selected. Array elements can only be selected using an integer index. Please see https://aka.ms/logicexpressions for usage details.'.

 

问题分析

从错误消息(Array elements can only be selected using an integer index)分析,因为 triggerBody() 函数获取的对象为一个Array(数组),所以无法查找到ObjectName属性。那么分析Request所传递的Body JSON结构后,发现:

1) Body中的最外层为一个数组,以中括号 [] 表示

2) Body中的第二层为一个对象,以大括号 {}表示

3) ObjectName属性位于data属性中,为第三层。

所以也无法使用 triggerBody()[0]?['objectName'] 加上数组索引后,依旧无法查找到ObjectName属性,错误信息如下:

InvalidTemplate. Unable to process template language expressions in action 'Send_message' inputs at line '0' and column '0': 'The template language function 'base64' expects its parameter to be a string, an object or an array. The provided value is of type 'Null'. Please see https://aka.ms/logicexpressions#base64 for usage details.'.

 

问题解决

所以为了解决JSON中数组结构的取数问题,可以使用 Logic App的 Select Action 组件来完成数据的提取,但是如果能确定 HTTP Request Body中的数组只有一个元素,则可以在表达式中指定索引号或使用fist(), last()等函数获取数组中的对象。然后通过['属性名'] 来一级一级的获取数据,如本文中获取ObjectName属性值得正确表达式为:

triggerBody()[0]?['data']['objectName']


//或者


first(triggerBody())?['data']['ObjectName']

 

注:表达式中的“?”号表示如果triggerBody()[0]对象如果返回null或者undefined时,就直接返回null或 undefined,JSON操作不在执行后续对象data,objectName的查找

 

 

测试动画

1) 在Logic App中测试

2) 在Service Bus Topic中验证消息是否正确传递

 

 

 

参考资料

有关在 Azure 逻辑应用的表达式中使用函数的参考指南 : https://docs.microsoft.com/zh-cn/azure/logic-apps/workflow-definition-language-functions-reference#triggerBody

 

相关文章
|
14天前
|
存储 JSON API
淘系API接口(解析返回的json数据)商品详情数据解析助力开发者
——在成长的路上,我们都是同行者。这篇关于商品详情API接口的文章,希望能帮助到您。期待与您继续分享更多API接口的知识,请记得关注Anzexi58哦! 淘宝API接口(如淘宝开放平台提供的API)允许开发者获取淘宝商品的各种信息,包括商品详情。然而,需要注意的是,直接访问淘宝的商品数据API通常需要商家身份或开发者权限,并且需要遵循淘宝的API使用协议。
淘系API接口(解析返回的json数据)商品详情数据解析助力开发者
|
3天前
【Azure Logic App】使用Event Hub 连接器配置 Active Directory OAuth 认证无法成功连接到中国区Event Hub的解决之法
An exception occurred while retrieving properties for Event Hub: logicapp. Error Message: 'ClientSecretCredential authentication failed: AADSTS90002: Tenant 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' not found. Check to make sure you have the correct tenant ID and are signing into the correct cloud. Che
|
18天前
|
C++
【Azure Logic App】使用Event Hub 连接器配置 Active Directory OAuth 认证无法成功连接到中国区Event Hub
【Azure Logic App】使用Event Hub 连接器配置 Active Directory OAuth 认证无法成功连接到中国区Event Hub
|
18天前
【Azure Logic App】在逻辑应用中开启或关闭一个工作流是否会对其它工作流产生影响呢?
【Azure Logic App】在逻辑应用中开启或关闭一个工作流是否会对其它工作流产生影响呢?
|
18天前
|
存储 SQL JSON
【Azure Logic App】微软云逻辑应用连接到数据库,执行存储过程并转换执行结果为JSON数据
【Azure Logic App】微软云逻辑应用连接到数据库,执行存储过程并转换执行结果为JSON数据
【Azure Logic App】微软云逻辑应用连接到数据库,执行存储过程并转换执行结果为JSON数据
|
5天前
|
存储 JSON API
Python编程:解析HTTP请求返回的JSON数据
使用Python处理HTTP请求和解析JSON数据既直接又高效。`requests`库的简洁性和强大功能使得发送请求、接收和解析响应变得异常简单。以上步骤和示例提供了一个基础的框架,可以根据你的具体需求进行调整和扩展。通过合适的异常处理,你的代码将更加健壮和可靠,为用户提供更加流畅的体验。
25 0
|
17天前
|
JSON API 数据格式
基于服务器响应的实时天气数据进行JSON解析的详细代码及其框架
【8月更文挑战第25天】这段资料介绍了一个使用Python从服务器获取实时天气数据并解析JSON格式数据的基本框架。主要分为三个部分:一是安装必要的`requests`库以发起HTTP请求获取数据,同时利用Python内置的`json`库处理JSON数据;二是提供了具体的代码实现,包括获取天气数据的`get_weather_data`函数和解析数据的`parse_weather_data`函数;三是对代码逻辑进行了详细说明,包括如何通过API获取数据以及如何解析这些数据来获取温度和天气描述等信息。用户需要根据实际使用的天气API调整代码中的API地址、参数和字段名称。
|
18天前
【Azure Standard Logic App】Workflow积压非常严重的情况下, 执行实例居然不能自动缩放的原因?
【Azure Standard Logic App】Workflow积压非常严重的情况下, 执行实例居然不能自动缩放的原因?
|
18天前
|
安全 API 网络架构
【Azure Logic App】使用 Easy Auth 在标准逻辑应用(Standard Logic App)中触发工作流
【Azure Logic App】使用 Easy Auth 在标准逻辑应用(Standard Logic App)中触发工作流
|
14天前
|
监控 网络协议 Java
Tomcat源码解析】整体架构组成及核心组件
Tomcat,原名Catalina,是一款优雅轻盈的Web服务器,自4.x版本起扩展了JSP、EL等功能,超越了单纯的Servlet容器范畴。Servlet是Sun公司为Java编程Web应用制定的规范,Tomcat作为Servlet容器,负责构建Request与Response对象,并执行业务逻辑。
Tomcat源码解析】整体架构组成及核心组件

热门文章

最新文章

推荐镜像

更多