AnsweredAssumed Answered

How to extract a value from JSON response using Java script in CA DEVTEST

Question asked by ahmmo05 Employee on Jun 13, 2017
Latest reply on Jun 14, 2017 by ahmmo05

Hi,

 

I have following JSON response:

 

{"message": "Successfully added Consumer with a given ConsumerId: consumer-6"}

 

To extract consumer-6 from the above response I have written following Java script to extract it.

 

var response = JSON.parse('{"message": "Successfully added Consumer with a given ConsumerId: consumer-6"}');
response.message ; 

 

On executing it throws following error, can someone help him?

 

============================================================================
| Error in Script
============================================================================
| Step: Java Script Step
----------------------------------------------------------------------------
| Message: Sourced file: inline evaluation of: ``var response = JSON.parse('{"message": "Successfully added Consumer with a given . . . '' Token Parsing Error: Lexical error at line 1, column 29. Encountered: "\"" (34), after : "\'{": <at unknown location>

----------------------------------------------------------------------------
| Trapped Exception: Sourced file: inline evaluation of: ``var response = JSON.parse('{"message": "Successfully added Consumer with a given . . . '' Token Parsing Error: Lexical error at line 1, column 29. Encountered: "\"" (34), after : "\'{": <at unknown location>

| Trapped Message: bsh.EvalError: Sourced file: inline evaluation of: ``var response = JSON.parse('{"message": "Successfully added Consumer with a given . . . '' Token Parsing Error: Lexical error at line 1, column 29. Encountered: "\"" (34), after : "\'{": <at unknown location>

----------------------------------------------------------------------------
STACK TRACE
bsh.EvalError: Sourced file: inline evaluation of: ``var response = JSON.parse('{"message": "Successfully added Consumer with a given . . . '' Token Parsing Error: Lexical error at line 1, column 29. Encountered: "\"" (34), after : "\'{": <at unknown location>

at bsh.Interpreter.eval(Interpreter.java:718)
at bsh.Interpreter.eval(Interpreter.java:758)
at bsh.Interpreter.eval(Interpreter.java:747)
at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:636)
at com.itko.lisa.test.ScriptExecHandler.executeScriptOld(ScriptExecHandler.java:528)
at com.itko.lisa.test.ScriptNode._execute(ScriptNode.java:126)
at com.itko.lisa.test.ScriptNode.execute(ScriptNode.java:103)
at com.itko.lisa.test.TestNode.executeNode(TestNode.java:981)
at com.itko.lisa.test.TestCase.execute(TestCase.java:1288)
at com.itko.lisa.test.TestCase.execute(TestCase.java:1203)
at com.itko.lisa.test.TestCase.executeNextNode(TestCase.java:1188)
at com.itko.lisa.editor.WalkThruPanel.prepAndExecNode(WalkThruPanel.java:1058)
at com.itko.lisa.editor.WalkThruPanel.access$900(WalkThruPanel.java:71)
at com.itko.lisa.editor.WalkThruPanel$10.doCallback(WalkThruPanel.java:965)
at com.itko.util.swing.panels.ProcessingDialog$2.run(ProcessingDialog.java:194)
at java.lang.Thread.run(Unknown Source)
Caused by: bsh.TokenMgrError: Lexical error at line 1, column 29. Encountered: "\"" (34), after : "\'{"
at bsh.ParserTokenManager.getNextToken(ParserTokenManager.java:2147)
at bsh.Parser.jj_scan_token(Parser.java:5934)
at bsh.Parser.jj_3R_142(Parser.java:4393)
at bsh.Parser.jj_3R_133(Parser.java:4400)
at bsh.Parser.jj_3R_101(Parser.java:4763)
at bsh.Parser.jj_3R_60(Parser.java:4743)
at bsh.Parser.jj_3R_34(Parser.java:4820)
at bsh.Parser.jj_3R_110(Parser.java:3890)
at bsh.Parser.jj_3R_73(Parser.java:3950)
at bsh.Parser.jj_3R_40(Parser.java:3942)
at bsh.Parser.jj_3R_138(Parser.java:3961)
at bsh.Parser.jj_3R_109(Parser.java:3971)
at bsh.Parser.jj_3R_72(Parser.java:3989)
at bsh.Parser.jj_3R_38(Parser.java:4775)
at bsh.Parser.jj_3R_104(Parser.java:4708)
at bsh.Parser.jj_3R_60(Parser.java:4749)
at bsh.Parser.jj_3R_34(Parser.java:4820)
at bsh.Parser.jj_3_8(Parser.java:3884)
at bsh.Parser.jj_2_8(Parser.java:3741)
at bsh.Parser.Expression(Parser.java:1085)
at bsh.Parser.VariableInitializer(Parser.java:624)
at bsh.Parser.VariableDeclarator(Parser.java:552)
at bsh.Parser.TypedVariableDeclaration(Parser.java:3359)
at bsh.Parser.BlockStatement(Parser.java:2809)
at bsh.Parser.Line(Parser.java:200)
at bsh.Interpreter.Line(Interpreter.java:1014)
at bsh.Interpreter.eval(Interpreter.java:651)
... 15 more
============================================================================

Outcomes