Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

WitableDataSource: Options to put null value when element is not there

Parasofttoudaya
Parasofttoudaya Posts: 232 ✭✭
edited September 2017 in SOAtest

Using Writable data source for JSON Response. In a JSON Response array contains two elements which i am writing in the writable data source. Where 1 element is present only in couple of places. I would like to write in the writable source at the same place.

In the given example enrollmentID 1,5,7 should goes to the corresponding row.

Note: Enrollment has been failure for 2,3,4,6. I have this to be get validated in anothe rtest suite by looping thru :(

Regards
Udaya

Comments

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭

    Team,

    Any solution on this???

  • Ireneusz Szmigiel
    Ireneusz Szmigiel Posts: 227 ✭✭✭
    edited September 2017

    @Parasofttoudaya
    I'm assuming, that you are using JSON DataBank.
    If yes, please see section "Handling Empty/Missing Elements to Maintain the
    Integrity of the JSON Response" in SOAtest manual/help.

    You should also take a look on Options settings for JSON DataBank:
    * Extract empty element as:
    * Extract missing element as:

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭

    @Irene

    Thanks for the response
    Example is showing like below,
    {
    "e": 5,
    "e": "",
    "e": 6
    }

    My example is like below
    [
    {
    "e": 1
    },
    {

    },
    {
    "e": 3
    },
    {

    }
    ]

    In this json array 2nd and 3rd position will not have values in it.. but other columns use to be present..

    Anything will work out??

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭

    Currently, you would have to pre-process the JSON, injecting "null" or "" (or whatever placeholder you desire) in into the places where the "EnrollmentID" property is missing. This way, your XPath that is extracting all EnrollmentIDs would return a consistent number of nodes. You could do this injection with scripting (Extension tool) or using XSLT Tool (with XML Converter tool). I don't have any examples of either approaches but this is what you have to do at a high level.

    I say "currently" because there are XPath expressions that can do what you need, returning some placeholder for missing values like for $item in (/root/myArray/item[*]) return if ($item/EnrollmentID/text()) then $item/EnrollmentID/text() else "". However, the built-in Data Bank and Transformer tools currently support XPaths that return nodes from the original document and not strings or a combination of nodes and strings. The built-in tooling won't be able to handle this for you. You would need to script this or use XSLT as mentioned.

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭
    edited September 2017

    for $item in (/root/item[*])
    return if ($item/id/text())
    then $item/id/text()
    else "null"

    It returns only one value null, But our expectation is supposed to be like belolw
    EnrollmentID1
    null
    EnrollmentID2

    null

    for $item in (/root/item[*])
    return
    ( if (1) then
    $item[1]/id[1]/text()
    else "null")

    result


    UIFccgbh

    OXxGLyC

    for $item in (/root/item[*])
    return
    ( if (0) then
    $item[1]/id[1]/text()
    else "null")

    result


    null

    Please advise

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭

    for $item in (/root/item[*])
    return
    if ($item/id[1]/text()) then
    $item/id[1]/text()
    else "null"

    I tried above one and am able to get only "null" in the evaluation.
    Note: Tried writing data source and got the same.


    for $item in (/root/item[*])
    return
    if (1) then
    $item/id[1]/text()
    else "null"

    result

    UIFccgb
    OXxGLy
    ------------------------------------------------------------------------------------------------------
    
    for $item in (/root/item[*]) 
    return
     if (0) then 
    $item/id[1]/text()
    else "null"
    
    result
    

    null

    Looks like return is happening at once, not for each ...

    please advice the tool option .

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭
    edited September 2017

    I'm afraid I do not understand your last comment at all. :) The XPath I mentioned is something that won't work in the SOAtest tools, like the Data Bank, for the reason I mentioned in my last comment. So, you shouldn't be trying to use it there :) This was simply an aside.

    As I mentioned, would need to write a script or XSL to pre-process the message. This would be something custom, specific to the messages you are dealing with. I don't have any examples for you offhand but it involves a bit of coding. You can do anything with scripting, provided you are able to write the code you need. Scripting is helpful when built-in tooling can't accomplish exactly what you need.

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭

    oops, sorry my bad.
    I don't want to pre-process the json content. Rather would like to put the value in the write file using extension tool.

    is it possible to write in writableDataSource by mentioning the row number?? Because used data source of mine is different from my writableDataSource.

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭

    You have to use a Data Bank to write to the Writable Data Source. You can't do this directly from an Extension Tool. However, you can chain a Data Bank to an Extension Tool.

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭

    I will check on it.

    As per the tool expectation "missing element" should work, can you please raise this issue as bug??

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭

    "Missing element" means the XPath returned nothing. It is not what you want here. In your case, your XPath always returns a list of nodes, it is just the wrong list. That fancy XPath I mentioned (with "for" and "$item") would normally be a way to get the kind of list you are after but the Data Bank would need to be enhanced to support XPaths that return plain strings or a mix of nodes and strings.

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭

    Yup element is getting missed in one of the array.
    "Handling Empty/Missing Elements to Maintain the
    Integrity of the JSON Response"

    In help content, example explanation has given only for empty string. Didn't talk about the Missing Elements. Please check on it.

    It will happen often in rest service, Otherwise its very difficult to maintain the integrity. If not a bug, is it possible to add as a feature request??

    Regards
    Udaya

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭
    edited September 2017

    "Extract missing element as" is for XPaths that return an empty node list.
    "Extract empty element as" is for XPaths that return empty string.

    Neither one helps you here and docs probably need improvement as you seem to imply.

    It will happen often in rest service

    Yes, you are right. This is something that needs to be made easier or at least possible with built-in tooling. Parasoft is aware of this.

    You need to iterate through the nodes and do something special when certain children don't exist. Using an XPath expression with "for" "if" and "else" keywords, like the one I mentioned, almost works except not quite due to the product limitation I mentioned. For now, I think you have use a script to parse the JSON and programmatically loop through the array items in your JSON to return a different JSON with null (or placeholder value) injected for any optional properties that are absent.

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭

    Thanks for understanding. I appreciate the timely response.

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭

    Integrity is achieved.. Thanks for routing for loop :smile::smile:

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭

    Looks like you found a node you can return for the "else" case that happens to work for you. Thank you for sharing!

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭
    edited September 2017

    By any chance do you know how to use nested in xpath reg-ex??

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭

    In XPath, there are various string functions including "matches" which accepts a regular expression:
    https://www.w3.org/TR/xpath-functions/#func-matches