如何移除sum类型任务输出结果中的重复双引号?
去除Chainlink sum类型任务输出中的重复双引号
问题背景
在Chainlink外部cron任务配置中,sum类型任务的输出会带有额外嵌套双引号(如输出值为"\"0.03394837\""),但jsonparse等其他类型任务的输出格式正常(如"0.007436033")。这种异常格式会导致后续HTTP请求的参数解析出错。
任务配置如下:
type = "cron" schemaVersion = 1 name = "gasOp" forwardingAllowed = false maxTaskDuration = "0s" schedule = "CRON_TZ=UTC 0 */1 * * * *" observationSource = """ opt_gas [type=bridge name="gas" requestData="{\"id\": $(jobSpec.externalJobID), \"data\": { \"network\": \"opt\"}}"] opt_gas_maxFeePerGas [type=jsonparse path="data,maxFeePerGas"] opt_gas_maxPriorityFeePerGas [type=jsonparse path="data,maxPriorityFeePerGas"] opt_gas_baseFee [type=jsonparse path="data,baseFee"] opt_gas_estimatedFee [type="sum"] opt_gas->opt_gas_maxFeePerGas opt_gas->opt_gas_maxPriorityFeePerGas opt_gas->opt_gas_baseFee opt_gas_maxFeePerGas->opt_gas_estimatedFee opt_gas_maxPriorityFeePerGas->opt_gas_estimatedFee opt_gas_baseFee->opt_gas_estimatedFee evershop_opt [type="http" method=POST url="http://localhost:3000/api/tax/classes/gas" requestData="{\"300\": {\"id\": 300,\"price\": $(opt_gas_estimatedFee)}}" allowUnrestrictedNetworkAccess=true ] """
sum任务输出示例(异常):
{ "__typename": "TaskRun", "id": "98716e62-5c0b-49d2-8168-af059b9c657a", "createdAt": "2023-11-26T18:34:00.181724Z", "dotID": "opt_gas_estimatedFee", "error": null, "finishedAt": "2023-11-26T18:34:00.181766Z", "output": "\"0.03394837\"", "type": "sum" }
jsonparse任务输出示例(正常):
{ "__typename": "TaskRun", "id": "863c888e-a6cd-438b-9fb8-9bc2bc122f73", "createdAt": "2023-11-26T18:34:00.181629Z", "dotID": "opt_gas_maxPriorityFeePerGas", "error": null, "finishedAt": "2023-11-26T18:34:00.181706Z", "output": "0.007436033", "type": "jsonparse" }
解决方案
可以通过添加额外任务清理sum输出的嵌套引号,以下是两种可行方法:
方法1:用jsonparse解析sum输出
sum任务的输出本质是带引号的JSON字符串,直接用jsonparse任务解析其原始值即可:
- 在
observationSource中添加清理任务:
opt_gas_estimatedFee_clean [type=jsonparse path=""]
- 添加任务连接关系:
opt_gas_estimatedFee->opt_gas_estimatedFee_clean
- 修改HTTP请求的
requestData,使用清理后的变量:
requestData="{\"300\": {\"id\": 300,\"price\": $(opt_gas_estimatedFee_clean)}}"
方法2:用template任务替换引号
通过模板替换功能直接移除嵌套双引号:
- 在
observationSource中添加模板任务:
opt_gas_estimatedFee_clean [type=template template="{{ .Value | replace \"\\\"\" \"\" }}"]
- 添加任务连接关系,同方法1;
- 修改HTTP请求的
requestData使用清理后的变量,同方法1。
最终修改后的完整配置示例(以方法1为例)
type = "cron" schemaVersion = 1 name = "gasOp" forwardingAllowed = false maxTaskDuration = "0s" schedule = "CRON_TZ=UTC 0 */1 * * * *" observationSource = """ opt_gas [type=bridge name="gas" requestData="{\"id\": $(jobSpec.externalJobID), \"data\": { \"network\": \"opt\"}}"] opt_gas_maxFeePerGas [type=jsonparse path="data,maxFeePerGas"] opt_gas_maxPriorityFeePerGas [type=jsonparse path="data,maxPriorityFeePerGas"] opt_gas_baseFee [type=jsonparse path="data,baseFee"] opt_gas_estimatedFee [type="sum"] # 添加清理任务 opt_gas_estimatedFee_clean [type=jsonparse path=""] opt_gas->opt_gas_maxFeePerGas opt_gas->opt_gas_maxPriorityFeePerGas opt_gas->opt_gas_baseFee opt_gas_maxFeePerGas->opt_gas_estimatedFee opt_gas_maxPriorityFeePerGas->opt_gas_estimatedFee opt_gas_baseFee->opt_gas_estimatedFee # 连接sum任务到清理任务 opt_gas_estimatedFee->opt_gas_estimatedFee_clean evershop_opt [type="http" method=POST url="http://localhost:3000/api/tax/classes/gas" # 使用清理后的变量 requestData="{\"300\": {\"id\": 300,\"price\": $(opt_gas_estimatedFee_clean)}}" allowUnrestrictedNetworkAccess=true ] """
内容的提问来源于stack exchange,提问作者Linux Issues
相关产品推荐
相关产品推荐

