Service Virtualization

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

    Broadcom Employee
    Posted Jun 13, 2017 11:42 AM

    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
    ============================================================================



  • 2.  Re: How to extract a value from JSON response using Java script in CA DEVTEST

    Posted Jun 13, 2017 12:40 PM

    Are you able to do something like the below using a JSR-223 step and Beanshell?

    NOTE: the code fragment below will extract the entire contents of the 'message' element, not the ID of the Consumer contained in the value. More code needed to get the actual consumer ID value if that is what you are after.

     

    import org.json.*;

    JSONObject json = new JSONObject( "{\"message\": \"Successfully added Consumer with a given ConsumerId: consumer-6\"}" );
    String response = json.get( "message" );

     

    //to see output in Events tab during ITR uncomment logger

    //_logger.info( "The value of message key is: {}", response );

     

    // return the value of the message element as the response
    return response;



  • 3.  Re: How to extract a value from JSON response using Java script in CA DEVTEST

    Broadcom Employee
    Posted Jun 13, 2017 12:59 PM

    Hi,

     

    Yes I need to extract the consumer id from the response and store in a variable so that I can use this variable in other step.

     

    Below is the screen shot and I need to parse the LASTRESPONSE object which has the following message:

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

     



  • 4.  Re: How to extract a value from JSON response using Java script in CA DEVTEST
    Best Answer

    Posted Jun 13, 2017 03:28 PM

    Technically speaking, the get method on the json object is not going to return this. You can add a little bit of script to substring the value out.  For example, 

     

    import org.json.*;

    JSONObject json = new JSONObject( "{\"message\": \"Successfully added Consumer with a given ConsumerId: consumer-6\"}" );
    String response = json.get( "message" );

    int sPos = response.indexOf("ConsumerId: ");
    String id;

    if ( sPos == -1 ) {
       // Consumer ID was not found in message
       id = "Not Found";
    } else {
       if ( sPos + 13 > response.length() ) {
          // add safety measure to stop index out of bounds
          id = "Not Included";
       } else {
          // if more stuff occurs after ConsumerId,
          // different logic applies
          id = response.substring( sPos+12 );
       }
    }

    // place in a property for later use

    testExec.setStateValue("fl_consumerId", id);

    // or return as lastresponse and store in a property

    return id;



  • 5.  Re: How to extract a value from JSON response using Java script in CA DEVTEST

    Broadcom Employee
    Posted Jun 14, 2017 07:36 AM

    Thanks it was helpful