0

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 ?

2
  • Try to use jsonSlurper instead of jsonArray Commented Feb 10, 2022 at 10:50
  • @daggett the slurper complains about he same parse errors Commented Feb 10, 2022 at 10:52

2 Answers 2

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]()
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure to well understand your regex, but it works well :) I would have prefered to not modify xml but it is suffisant for me ; thank you very much !
0

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"}

2 Comments

as in my real case, I got a String directly by API, I cannot encode \ with \\ as they are already like that in apiReply.getResponseText()
Then show what in response of your API and how you are parsing it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.