使用JavaScript仅转义JSON对象cData字段值双引号,适配Logic App内联使用
JavaScript实现方案
核心思路
原始输入属于非法JSON格式(cData字段内部双引号未转义),无法直接解析,因此通过正则精准定位cData字段的内容,仅对该字段内部的双引号做转义处理,全程无额外循环,适配Logic App内联脚本的轻量化要求。
可用代码片段
// 替换为你实际获取的原始输入字符串即可 const rawInput = `{"id":2097,"date":"2021-12-03T00:00:22","name":"data-load","cData":"This game is the most played game inside "USA". If you are also sure of your luck, then you must take this ticket. If you have "good luck", then the next first prize can be "yours"."}`; // 精准匹配cData字段并转义内部双引号 const processedStr = rawInput.replace(/"cData":"(.*)"\s*}/g, (_, cDataVal) => { return `"cData":"${cDataVal.replace(/"/g, '\\"')}"`; }); // 处理完成后可直接转为合法JSON对象供后续步骤使用 const finalResult = JSON.parse(processedStr);
注:该代码适用前提为输入除cData字段内部双引号未转义外,其余JSON结构均合法,且cData为JSON的最后一个字段,符合你当前给出的输入场景。如果cData不是最后一个字段,将正则替换为
/"cData":"(.*)"\s*,/即可。
效果验证
处理后的processedStr完全符合你给出的预期输出格式,仅cData字段内的双引号被转义,其他字段内容无任何修改。
内容的提问来源于stack exchange,提问作者Karl
相关产品推荐
相关产品推荐

