'Groovy JSONArray : org.json.JSONException: when decoding string with \"
I get no way to parse this Json which seems to be valid for me :
def diffOfApi='''[{"op":"replace","path":"/data/0/messageBody","value":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<WCS reference=\"336042800000431486\"><PREPARATION_REQUEST requestId=\"WDP_PICKVRAC3\" date=\"2022-02-08 18:33:39\"><CONTAINERS><CONTAINER containerId=\"336042800000431486_21_21121\" handle=\"CREATE\" wmsOrderId=\"WDP_PICKVRAC3\" site=\"CPEEPF\" status=\"CREATED\" referenceDate=\"2022-02-08 18:33:39\" purgeAuthorized=\"false\"><BARCODES><BARCODE main=\"false\" reusable=\"false\">336042800000431486</BARCODE></BARCODES><PACKAGING>TX</PACKAGING><WEIGHT_CHECKING mode=\"NO\"/></CONTAINER></CONTAINERS></PREPARATION_REQUEST></WCS>"}]'''
JSONArray jsonArray = new JSONArray(diffOfApi);
I get this error :
Reason:
org.json.JSONException: Expected a ',' or '}' at 71 [character 72 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
at org.json.JSONObject.<init>(JSONObject.java:229)
at org.json.JSONTokener.nextValue(JSONTokener.java:363)
at org.json.JSONArray.<init>(JSONArray.java:115)
at org.json.JSONArray.<init>(JSONArray.java:144)
at TEST.run(TEST:32)
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:442)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:433)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:412)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:404)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:281)
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 TempTestCase1644483473725.run(TempTestCase1644483473725.groovy:25)
I think it is due to " encapsulated xml in item value
Any idea to make it working ?
Solution 1:[1]
You can replace the \" characters with ' to allow for JSON- and later XML-processing:
import groovy.json.*
import groovy.json.*
def diffOfApi='''[{"op":"replace","path":"/data/0/messageBody","value":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<WCS reference=\"336042800000431486\"><PREPARATION_REQUEST requestId=\"WDP_PICKVRAC3\" date=\"2022-02-08 18:33:39\"><CONTAINERS><CONTAINER containerId=\"336042800000431486_21_21121\" handle=\"CREATE\" wmsOrderId=\"WDP_PICKVRAC3\" site=\"CPEEPF\" status=\"CREATED\" referenceDate=\"2022-02-08 18:33:39\" purgeAuthorized=\"false\"><BARCODES><BARCODE main=\"false\" reusable=\"false\">336042800000431486</BARCODE></BARCODES><PACKAGING>TX</PACKAGING><WEIGHT_CHECKING mode=\"NO\"/></CONTAINER></CONTAINERS></PREPARATION_REQUEST></WCS>"}]'''
diffOfApi = diffOfApi.replaceAll( /=.{1}([^"]+)"/, "='\$1'" )
def jsonArray = new JsonSlurper().parseText diffOfApi
assert 3 == jsonArray.first().size()
def xml = new XmlSlurper().parseText jsonArray.first().value // root element points to <WCS/>
assert 'WDP_PICKVRAC3' == [email protected]()
Solution 2:[2]
you have incorrectly encoded json string.
if json contains doublequote in a value " - it must be encoded as \" but inside the groovy/java code each \ also must be encoded with \\
so when you have following in code:
def x = '''{"x":"abc\"def"}'''
then actual x value at runtime would be
{"x":"abc"def"}
that is incorrect json
so, x should be defined as
def x = '''{"x":"abc\\"def"}'''
to have runtime value
{"x":"abc\"def"}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | injecteer |
| Solution 2 | daggett |
