How to read JSON response with JYTHON script.

NeilNeil Posts: 11

Hello,

I am trying to read JSON response via JYTHON script. h

def assertresponse(input, context):
value = String(input)
#value = input.scannedDate()
Application.showMessage(value)

The above script will read complete response.
1)I want to know how I can read individual field in response and load the value into a variable.
2) if there are multiple occurrences of same field, how to get 1st occurrence value in the response?

I am not restricted to Jython, if there is any other script which can give me the same output. I am fine.

Thanks in advance,

Regards,
Suhas

Happy Holidays

Comments

  • Thomas MooreThomas Moore Posts: 25 admin

    Hi Suhas,

    I'm not sure what release of SOAtest you're using, but going back several major versions, we have access to a JSON Databank tool that will acheive your exact scenario without scripting.

    Attaching a JSON Databank to the response traffic of your messaging client will give you a graphical representation of the response traffic that you can use to extract the exact values you are looking for.

    Hope this helps!

    Thomas

  • NeilNeil Posts: 11

    Hi Thomas,

    Thanks for your response.

    I know there is a data bank where we can load the response value into a variable and use the same for future tests.

    But my requirement was not just to load into a variable , But also want to compare the two fields are sorted alphabetically A-Z, for which I am using jython script.

    with jython, I am able to read complete response , But I want to know how to read single field in response.

    Thanks,
    suhas

  • NeilNeil Posts: 11

    I am using SOA test 9.9

  • Thomas MooreThomas Moore Posts: 25 admin
    edited December 2016

    Hi Suhas,

    In that case, there will have to be a bit more logic that takes place here. In order to verify the alphabetical sorting of JSON elements, you can use a JSON Assertor with a custom assertion, as sort verification is not built in to SOAtest.

    To do this, I used a JSON Databank as before to extract all of the values that I am looking for. This time, however, I extracted them into a Writable Datasource. Make sure when you add your datasource that the Append radio button next to Standard Test Modes is selected.

    After the Datasource is set up, you can then choose the location of where you want your JSON Databank to store them.

    In this case, every element we wish to extract should be added into that Writable data source column.

    After that setup is done, you can add the custom assertion into the JSON Assertor. Make sure when you create the assertion that it only applies to one element.

    The Groovy script I used to verify that my values are sorted is as follows:

    import com.parasoft.api.*;
    public boolean compareASC(Object input, ScriptingContext context)
    {
        ArrayList<String> list = context.getValues("data","values");
        for(int i = 0; i < list.size() - 1; i++)
        {
            String a = list.get(i);
            String b = list.get(i+1);
            if(a.compareTo(b) > 0)
                return false;
        }
        return true;
    }

    Keep in mind that "data" is the name of my Writable Data source and "values" is the name of the column where I am storing that data. If you want to make a custom assertion for descending order, all you need to do is change the greater than to a less than in the compareTo call.

    Let me know if any of the steps are unclear, or if this helps you out!

    -Thomas

  • NeilNeil Posts: 11

    Hi Thomas,

    Happy New Year..:)
    sorry for the late response. I was on vacation.

    I will use your logic and see. Thank you so much for your time and efforts.

    regards,
    Neil

  • NeilNeil Posts: 11

    Hi Thomas,

    As mentioned , I did wrote the filenames to writable data source .
    please see my writable data source configuration below:

    can you please help me resolve below errors:
    1. The test is running 8 times(I dont want to RUN 8 times, I just want to compare the filenames are ordered ASC).
    2. If two elements are of same name, then how the ASC logic works?
    3. The same Groovy script also works for Date fields? if I want to check the date fields are in ordered ASC?

    Thanks for your time.

    Regards,
    Suhas

  • Thomas MooreThomas Moore Posts: 25 admin

    Hi Suhas,

    With regards to your questions:

    1. The test is running 8 times(I dont want to RUN 8 times, I just want to compare the filenames are ordered ASC).

    I addressed this behavior in my original answer, but to reiterate:

    After that setup is done, you can add the custom assertion into the JSON Assertor. Make sure when you create the assertion that it only applies to one element.

    If you select apply to all elements, then it will run the test once per element as opposed to once for all elements.

    1. If two elements are of same name, then how the ASC logic works?

    In this case, the compareTo logic will fail, however to fix this, all you need to do is change the > to >= in the compareTo if statement.

    1. The same Groovy script also works for Date fields? if I want to check the date fields are in ordered ASC?

    Yes, but only if the dates contain leading zeroes for single digit dates, ex: 2017-01-01 instead of 2017-1-1.

    Hope this helps!

    Thomas

  • kktulasi404kktulasi404 Posts: 10

    Hi Thomas,

    I have the similar situation as posted here.
    Requirement:
    1. Execute a REST client with different inputs (the Inputs are been passed from a Excel Data Source)
    2. Compare each output with the expected result. All the expected results are been saved in a Excel data source
    Ex: When Input1 passed the output has three records with three columns.
    For Input2 passed the output has a different set of three records with three columns.
    Now compare the Input1 results with the first 3 rows of excel and Input2 results with the 4,5,6 rows.
    3. I have executed the API with Input1 and Input2 and saved the results in a writable JSON data bank.. All the data is been saved.
    ISSUE: On actual results - If input1 returned only 2 rows and Input2 returned 4 rows then how will I know which records for Input1 and which are for Input2.
    4. How can I compare the Actual results stored in Writable Data Source with the Expected results.
    Tried using the Messaging Client as next test case and use both the files. But error saying invalid JSON

    Thank you,
    Tulasi

  • Thomas MooreThomas Moore Posts: 25 admin

    Hi Tulasi,

    Sorry for the slow response on this. Your requirement is a bit unclear to me. Is my understanding of it correct?

    You receive a response that is structured similar to the screenshot you provided, and you want to ensure that those values are equal to a fixed set of expected values?

    Also, when these values are in the excel spreadsheet, they are in a single row?

    If this is the case, would it be possible for you to split values between columns? For example, you could add a new column to the Writable Datasource by right clicking on the column header (A), and selecting Insert Columns. Once you have done this, you could use one column for each of the sections (input1, input2, etc.)

    In your script, you can then edit it so that after it finishes iterating through the first set of values from input1, it then moves to input2 and validates those against the remaining rows in the expected values datasource.

    Does the scenario I put forth make sense?

    Thomas

Sign In or Register to comment.