如何使用JOLT转换JSON、设置默认值并处理error-response节点
JOLT转换实现方案
输入JSON
{ "error-response": [ { "code": 1401, "status": "No TITLE Found in request" }, { "code": 1402, "status": "No FIRST_NAME Found in request" }, { "code": 1403, "status": "No LAST_NAME Found in request" }, { "code": 1404, "status": "No EMAIL ID Found in request" } ] }
JOLT规格
[ // 提取关键信息:标记1401是否存在,收集所有错误描述 { "operation": "shift", "spec": { "error-response": { "*": { "code": { "1401": { "@(2,status)": "1401_msg", "#true": "has_1401" } }, "status": "error_list[]" } } } }, // 根据1401的存在状态设置最终状态和详情 { "operation": "modify-overwrite-beta", "spec": { "has_1401": ["=notNull", false], "status": ["=cond", ["eq", ["@(1,has_1401)", true], "REJECT", "ERROR"]], "status_detail": ["=cond", ["eq", ["@(1,has_1401)", true], "@(1,1401_msg)", ["=join", ",", "@(1,error_list)"]]] } }, // 添加固定字段并清理临时数据 { "operation": "shift", "spec": { "status": "status", "status_detail": "status_detail", "#null": ["monthly_repayment", "accept_token", "total_charge", "offered", "approval_probability", "first_repayment", "lender_branch", "total", "phone"], "#Unsecured": "product_type" } } ]
规格说明
- 第一阶段(shift操作):遍历
error-response数组,捕获code=1401对应的错误描述并标记存在状态,同时收集所有错误描述到数组中。 - 第二阶段(modify-overwrite-beta操作):
- 给
has_1401设置默认值false(当数组中无1401项时生效); - 通过条件判断输出
status:存在1401则返回REJECT,否则返回ERROR; - 同理生成
status_detail:存在1401则返回对应描述,否则用逗号拼接所有错误描述。
- 给
- 第三阶段(shift操作):保留最终状态字段,批量添加要求的
null值字段,设置固定的product_type,同时清理过程中产生的临时字段。
输出验证
- 当输入包含1401项时,输出为:
{ "status": "REJECT", "status_detail": "No TITLE Found in request", "monthly_repayment": null, "accept_token": null, "total_charge": null, "offered": null, "product_type": "Unsecured", "approval_probability": null, "first_repayment": null, "lender_branch": null, "total": null, "phone": null }
- 当输入无1401项时,输出与你提供的期望输出完全一致。
内容的提问来源于stack exchange,提问作者Mega
相关产品推荐
相关产品推荐

