Accessing JSON data bank from Jython

aa_dfwaa_dfw Posts: 4

How to access element from JSON data bank via jython, manipulate it (remove special characters from the string) and save it back to the data bank.

Comments

  • Ramiro MartinezRamiro Martinez Posts: 47 admin

    Hi aa_dfw,

    You can save the values in to a test suite variable and then using the Parasoft api methods:
    context.getValue("variable_name") and context.setValue("variable_name","new_value"); to pull and set the new values.

    Example script:
    import com.parasoft.api.*;

    public void exampleScript(Object input, ExtensionToolContext context){
    
        //Application.showMessage(string) 
    
        //This is grabbing the variable x(If the varible exist) from the test suite variables and displays it in the console window. 
        Application.showMessage(context.getValue("x"));
    
        //This will set a new value for x test suite variable named x
        context.setValue("x","1000");
        Application.showMessage(context.getValue("x"));
    
        //This will print out the input in this example the input was set in the extension tool
        //If chaining from another tool such as a messaging client the output of the tool will be the Object input
        Application.showMessage(input.toString());
    }
    
  • OmarROmarR Posts: 186 admin
    edited August 2017

    Señor aa_dfw,

    An alternative solution to @Ramiro Martinez's lovely script would be to use the "Allow alteration" option available under JSON databank >options.

    This option allows you to alter specific element-values and store them in the databank.

    After you've altered the element, you can extract the same element (under Extract tab) and use the updated value in subsequent tests.

  • aa_dfwaa_dfw Posts: 4
    edited August 2017

    Hi OmarR,
    Thanks for your reply. This would be perfect, but I'm not sure how to perform the following modification of he current string: I need to replace all occurrences of "\" in the current string with "" (basically removing all "\" from the string. Any suggestions?

    Thanks,
    Amela

  • benken_parasoftbenken_parasoft Posts: 399 ✭✭✭
    edited August 2017

    A couple things to keep in mind. Please be aware that backslash is an escape character in JSON. When you extract a JSON string value, like with a JSON Data Bank, then any JSON escape sequences are automatically decoded. You don't need to do this manually by script or something. For example, backslash-double-quote (\") would automatically become a double-quote ("), backslash-t (\t) with the tab character, double-backslash (\\) with a single backslash (\), etc. The data bank column will have the logical string value, not way it needed to be encoded within a JSON document.

Sign In or Register to comment.