Forum Discussion

MFzOS's avatar
MFzOS
Occasional Contributor
3 years ago

Using Property Transfer in Ready API, How Can I Transfer a Numeric Value into JSON object?

Hi,

When fetching data source from EXCEL file, I need to transfer some of the values to JSON objects. All values are being parsed in as string format. How can I parse in values as integer instead when using property transfer step?

  • Hey MFzOS 

     

    Ok - so I've set it up and it runs fine - when I first did it, it auto generated quote marks around the numeric values that ive parameterized in my payload, but once I'd removed them it works fine

     

     

    have a look at the following screenshots:

     

     

    In the above screenshot you can see Im sourcing data from a csv using the Datasource (file) type.

    The first two values auto generate alpha numeric values, but column 3 includes a numeric and column 4 includes a string 1.0 is wrapped in quotes

     

    Next - if you look at the REST request in the screenshot below - in the Request tab and the payload editable field - you can see Im sourcing data from the Data Source test step.  

    if you look at the 'defraexp_ehcstatus' attribute value in the input payload above - the parameterized value is NOT wrapped in quotes (so it's numeric)

    If you look at the 'defraexp_ehcversion' attribute value in the input payload above - the parameterized value IS wrapped in quotes (so it's a string)

     

    Now - if you look at the next screenshot, you can see the RAW of the request - which shows what is submitted - in this case, the submitted payload maintained the attributes' datatypes relative to what was defined

     

     

    cool?

     

    Cheers,

     

    Rich

  • richie's avatar
    richie
    Community Hero

    Hey MFzOS 

     

    sorry - can you provide a bit more clarification

     

    I just need to be clear - you can use a toInteger() method and toString() methods to cast between the two types, however - quite often (especially with json) I've found I haven't needed to bother until I was asserting in script assertion or if I need to do some maths on the value (i.e. multiply the property value by 2 or whatever).

     

    if you're just building the content of the request and it's json - so datatypes are kinda maintained in json (where theyre not in xml) - then you dont really need to worry about datatypes too much, cos it's handled by the quote wrapping.

     

    for example.

     

    If you have data in a csv (which is pointed to via a DataSource test step) that provides specific json attributes with values, in the request payload it could look like the following:

     

    "attribute1" : "${DataSource#attribute1}"   <---value is sourced from the Datasource object (teststep) and the value will be a string cos the property is wrapped in quote marks

     

    whereas:

     

    "attribute2": ${DataSource#attribute1}   <--- here the value sourced from the Datasource object (teststep) would be a numeric - cos it's not wrapped in quote marks

     

     

    If I have a value saved to a property within a Properties test step, (all values when saved to a Properties step are saved as text/strings) and I wanted to retrieve that property value after casting to a numeric/integer, I'd use the following:

     

    def NumericAttribute1 = propertiesStep.getPropertyValue(attribute1).toInteger();

     

     

    Does that help?  If you can clarify a little more, I'm sure someone will be able to answer your question.

     

    Cheers,

     

    Rich

    • MFzOS's avatar
      MFzOS
      Occasional Contributor

      Hello Rich and thanks for your response!

      The thing is that I am not using groovy script when transferring data.

      First I read data from excel file where I store the values in properties, then I use Property Transfer step to transfer these properties to fields in a JSON array. 

      All values are transferred as string where some of the fields in the JSON are numeric. 

      In the example below you can see the transfer step I use to transfer the value of the property "QUANTITY" to qty in JSON where the array will look :

      {
         "data" : {
               "info" : {
                     "qty" : "1750"
                          }
                   }
      }

       

      Which is wrong because qty is numeric field and not string.

      • richie's avatar
        richie
        Community Hero
        Hey MFzOS,

        Ok. Weird....ive never seen someone bother using a property transfer step in between a Datasource step and a Request step before, cos you dont actually need to at all.....but you could teach me a new approach, so i need to just clarify!

        Themore i think about this, the more confused i get and im starting to think im missing something essential.

        am i understanding....you have a whole json payload in your datasource and youre using a property transfer to try and extract a numeric (which is part of the json payload), to then populate a specific attribute within a json payload that is hard coded into the editable field on your Request test step?

        Reason why ive never seen it done the way i think youre doing is cos the functionality in the Request form can source values from your Datasource step directly (there is still property transfer taking place...you just havent got an explicit step in between your steps doing it).

        For example...from what youve said it sounds like your testcase object hierarchy is as follows:

        TestSuite
        ---TestCase
        ------Datasource step
        ------PropertyTransfer step
        ------RestRequest step

        Whereas most people just do:

        TestSuite
        ---TestCase
        ------Datasource step
        ------RestRequest step

        The options im thinking of remove the hassle of you having to cast to numeric....its done as a consequence of your quote wrapping.

        Ok...so....im typing this out on my phone right now, but when im infront of my laptop, i'll demo out your scenario using my approach and screenshot it all, so you can see clearly what i mean, but just to confirm a couple of questions:

        Q1. Are you using Datasource type=file?

        Q2. The source excel file youre using.....are you including the whole json payload in there

        Q3. you have a whole json payload in your datasource and youre using a property transfer to try and extract a numeric (which is part of the json payload), to then populate a specific attribute within a json payload that is hard coded into the editable field on your Request test step?

        Sorry...i know explaining repeatedly can be tedious, but if o dont understand completely, i'd probably give you the wrong answer....so its worth nailing the details down up front.

        Cheers!

        Rich
  • MFzOS's avatar
    MFzOS
    Occasional Contributor

    Hi,

    It is type 'Excel' where I use a spreadsheet that contains my test data.

     

    Cheers,

  • MFzOS's avatar
    MFzOS
    Occasional Contributor

    Great! This is actually working. I just think it is a bit strange that when moving properties using Property Transfer or even entering values manually via Grid view it doesn't take the type of the attribute under consideration.

     

    Many thanks for your help, Rich! 

     

    Cheers! 

    • richie's avatar
      richie
      Community Hero

      Hey MFzOS 

       

      Yeah - is a bit weird - also - when I tried initially it inserted quote marks into my payload around the numerics automatically that I had to remove - even though there weren't any to begin with - that's not gonna help someone a little unfamiliar with the functionality - theyre just gonna struggle thinking it doesnt support numerics!

       

      I might have a play around with the functionality to work out in the ins and outs of it - as i said in the original message - values are saved as strings - but with a json payload - it's whether you've included quote marks in your payload that determines whether it's a string or numeric.

       

      Anyway - glad you got it sorted!

       

      Rich