Sebastien TANIERE
Sebastien TANIERE

Reputation: 46

groovy jsonbuilder remove json node

I try to remove a json node when it contains specific value. but I get an error. Goal is to remove an element from my json by checking its path if it contains a prefix and a suffix

could you help me to make my code working ?

import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
String pathPrefix = "/server_information/environment"
String pathSuffix = "/server_information/environment"

String diffOfApi = """[{op:replace, path:/server_information/environment, value:QCSGERFX023}, {op:replace, path:/json_detail/pick_batch/0/support_list/0/already_send, value:false}]"""    JsonSlurper slurper = new JsonSlurper()
def slurped = slurper.parseText(diffOfApi)
def parsedJsonDiff = new JsonBuilder(slurped)

println "removeDiffByPath() - avant removeAll parsedJsonDiff : $parsedJsonDiff"
//parsedJsonDiff.removeAll { it.path == "/json_detail/preparation_list/0/consignee/update_date" }
parsedJsonDiff.removeAll { it.path.contains(pathPrefix) && it.path.contains(pathSuffix) }
println "removeDiffByPath() - apres removeAll parsedJsonDiff : $parsedJsonDiff"

println parsedJsonDiff.toString()

for the moment, I get this error :

Test Cases/_DEBUG SEB/TEST groovy FAILED. Reason: groovy.json.JsonException: expecting '}' or ',' but got current char 'o' with an int value of 111

The current character read is 'o' with an int value of 111 expecting '}' or ',' but got current char 'o' with an int value of 111 line number 1 index number 2 [{op:replace, path:/server_information/environment, value:QCSGERFX023}, {op:replace, path:/json_detail/pick_batch/0/support_list/0/already_send, value:false}] ..^ at TEST groovy.run(TEST groovy:27) at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194) at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119) at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:430) at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:421) at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:400) at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:392) at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:273) at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142) at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133) at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source) at TempTestCase1637062445227.run(TempTestCase1637062445227.groovy:25)

Upvotes: 1

Views: 2215

Answers (1)

Sebastien TANIERE
Sebastien TANIERE

Reputation: 46

thanks to cfrick, I corrected diffOfApi json which were malformed (missing ""). Then I used jsonSlurper instead of jsonBuilder in order to use removeAll()

Here is working code :

import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
import groovy.json.JsonParserType

String pathPrefix = "/server_information/environment"
String pathSuffix = "/server_information/environment"
int i=0

//String diffOfApi = """[{op:replace, path:/server_information/environment, value:QCSGERFX023}, {op:replace, path:/json_detail/pick_batch/0/support_list/0/already_send, value:false}]"""
String diffOfApi = """[{"op":"replace", "path":"/server_information/environment", "value":"QCSGERFX023"}, {"op":"replace", "path":"/json_detail/pick_batch/0/support_list/0/already_send", "value":"false"}]"""
JsonSlurper slurper = new JsonSlurper()
//slurper.setType(JsonParserType.LAX)

def slurped = slurper.parseText(diffOfApi)
//def parsedJsonDiff = new JsonBuilder(slurped)

println "removeDiffByPath() - avant removeAll parsedJsonDiff : $slurped"
// on ne tient pas compte des modifs de date de consignee
slurped.each {println "slurped " + ++i + " "+it}
slurped.removeAll { it.path.contains(pathPrefix) && it.path.contains(pathSuffix) }

println "removeDiffByPath() - apres removeAll parsedJsonDiff : $slurped"

def parsedJsonDiff = new JsonBuilder(slurped)

println parsedJsonDiff.toPrettyString()

now I get this result :

2021-11-16 15:00:59.997 DEBUG testcase.TEST groovy                     - 12: println(parsedJsonDiff.toPrettyString())
[
    {
        "op": "replace",
        "path": "/json_detail/pick_batch/0/support_list/0/already_send",
        "value": "false"
    }
]

Upvotes: 1

Related Questions