Get the value from a property with a special character in the name

Hello guys.

I’m not able to get a value because the property name has special characters

example:
“Result”:{“@type”:“Result”}

If I do

WSBuiltInKeywords.verifyElementPropertyValue(response, ‘Result.@type’, ‘Something’)

or

WS.getElementPropertyValue(response,‘Result.@type’)

ERROR c.k.k.core.keyword.internal.KeywordMain - ? Unable to verify element property value (Root cause: groovy.lang.MissingFieldException: No such field: type for class: groovy.json.internal.LazyMap

ERROR c.k.k.core.main.WSVerificationExecutor - ? Verification FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: Unable to verify element property value (Root cause: groovy.lang.MissingFieldException: No such field: type for class: groovy.json.internal.LazyMap

Open to ideas hehe, thanks!

1 Like

Please use “Code Formatting syntax” for code, log, file content for better readability

3 Likes

This is not a valid JSON. It would never be properly processed by any JSON Parser.

I guess you wanted

{"Result":{"@type":"Result"}}
1 Like

You can find the source code of WS.verifyElementPropertyValue keyword at

You should read the source to investigate whatever problem about the keyword.

1 Like

I could reproduce the problem with the following code:

import com.kms.katalon.core.webservice.helper.WebServiceCommonHelper as WSCH

String content = '''{"Result":{"@type":"Value"}}'''

Object obj = WSCH.parseAndGetPropertyValueForJson('Result.@type', content)
println obj

Console output:

2023-09-07 05:59:47.261 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2023-09-07 05:59:47.264 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/TC1
2023-09-07 05:59:47.712 DEBUG testcase.TC1                             - 1: content = "{"Result":{"@type":"Value"}}"
2023-09-07 05:59:47.715 DEBUG testcase.TC1                             - 2: obj = parseAndGetPropertyValueForJson("Result.@type", content)
2023-09-07 05:59:47.804 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/TC1 FAILED.
Reason:
groovy.lang.MissingFieldException: No such field: type for class: groovy.json.internal.LazyMap
	at Script1.run(Script1.groovy:1)
	at com.kms.katalon.core.webservice.helper.WebServiceCommonHelper.parseAndGetPropertyValueForJson(WebServiceCommonHelper.java:243)
	at com.kms.katalon.core.webservice.helper.WebServiceCommonHelper$parseAndGetPropertyValueForJson.call(Unknown Source)
	at TC1.run(TC1:5)
	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:448)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:144)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:135)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1694033984090.run(TempTestCase1694033984090.groovy:25)

2023-09-07 05:59:47.832 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/TC1 FAILED.
Reason:
groovy.lang.MissingFieldException: No such field: type for class: groovy.json.internal.LazyMap
	at Script1.run(Script1.groovy:1)
	at com.kms.katalon.core.webservice.helper.WebServiceCommonHelper.parseAndGetPropertyValueForJson(WebServiceCommonHelper.java:243)
	at com.kms.katalon.core.webservice.helper.WebServiceCommonHelper$parseAndGetPropertyValueForJson.call(Unknown Source)
	at TC1.run(TC1:5)
	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:448)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:144)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:135)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1694033984090.run(TempTestCase1694033984090.groovy:25)

2023-09-07 05:59:47.866 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/TC1

``
1 Like

The following code worked:

import com.kms.katalon.core.webservice.helper.WebServiceCommonHelper as WSCH

String content = '''{"Result":{"@type":"Value"}}'''

Object obj = WSCH.parseAndGetPropertyValueForJson('Result."@type"', content)
println obj

Output:

2023-09-07 06:09:38.736 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2023-09-07 06:09:38.740 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/TC2
2023-09-07 06:09:39.200 DEBUG testcase.TC2                             - 1: content = "{"Result":{"@type":"Value"}}"
2023-09-07 06:09:39.202 DEBUG testcase.TC2                             - 2: obj = parseAndGetPropertyValueForJson("Result."@type"", content)
2023-09-07 06:09:39.312 DEBUG testcase.TC2                             - 3: println(obj)
Value
2023-09-07 06:09:39.343 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/TC2

See the doc of GPath for detail.

2 Likes

One more exercise:

import com.kms.katalon.core.webservice.helper.WebServiceCommonHelper as WSCH

String content = '''{"Result":{"@type":"Value"}}'''

Object obj = WSCH.parseAndGetPropertyValueForJson('Result["@type"]', content)
println "obj=" + obj

This worked fine:

2023-09-10 13:17:08.508 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2023-09-10 13:17:08.511 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/testGPath
Test Cases/testGPath
[:]
obj=Value
Test Cases/testGPath
PASSED
2023-09-10 13:17:09.154 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/testGPath

This proves that the expression 'Result["@type"]' is evaluated valid.

The expression 'Result["@type"]' looks familiar to me. It is just the same as a code in JavaScript that gets access to a property of an object.

On the other hand, the expression 'Result."@type"' looks puzzling to me. I have never seen this syntax in any other places.