Karate无法从嵌套JSON对象中移除structureInfo属性
Karate移除嵌套JSON属性失败的解决办法
我使用的Karate版本:karate-core、karate-junit5为1.3.0,karate-archetype为1.4.0.RC3。从文件读取JSON数据后,想要移除其中嵌套的structureInfo属性(该属性包含structureComponent、properties、fields等子字段),但尝试delete和remove命令均未生效。
尝试过的代码
* def showJson = read('classpath:Data/show.json') # 从文件读取JSON * if(true) delete showJson['body.structureInfo'] * if(true) delete showJson['showJsonbody.structureInfo']
原始JSON数据
{ "header": { "method": "GET", "response": { "type": "basic", "status": "success" }, "source": "#present" }, "session": "abc", "body": { "session": { "structuralComponent": { "id": 2000, "type": "tabs", "orderSeq": 0, "properties8i": { } } }, "structureInfo": { "structureComponent": { "id": 2000, "type": "tabs", "orderSeq": 0, "properties": { }, "fields": [ { "id": 2001, "label": "#regex General[ a-zA-Z0-9!@#$%^&*()]+", "type": "basic", "orderSeq": 10, "properties": { } } ] } }, "session1": { } } }
期望结果(移除structureInfo后)
{ "header": { "method": "GET", "response": { "type": "basic", "status": "success" }, "source": "#present" }, "session": "abc", "body": { "session": { "structuralComponent": { "id": 2000, "type": "tabs", "orderSeq": 0, "properties8i": { } } }, "session1": { } } }
正确解决方式
你之前的写法错误在于使用了字符串形式的复合路径索引,Karate中删除嵌套属性需要用正确的对象访问方式,以下两种写法均可生效:
方式1:直接通过点符号访问删除
* def showJson = read('classpath:Data/show.json') * delete showJson.body.structureInfo
方式2:使用多级对象索引删除
* def showJson = read('classpath:Data/show.json') * delete showJson['body']['structureInfo']
另外,你代码里的showJsonbody.structureInfo是拼写错误,多叠加了变量名,正确路径应为body.structureInfo。
内容的提问来源于stack exchange,提问作者karusai
相关产品推荐
相关产品推荐

