Using Property Transfer in Ready API, How Can I Transfer a Numeric Value into JSON object?
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello again!
First I want to thank for the effort you're putting to help me with this. I've been asking much lately about an easy way to cast a numeric value in a JSON attribute in ReadyAPI and I've not gotten the clear answer how to do that.
Here comes the answers to your questions:
A1. I'm using "Data Source Excel.xlsx".
A2. In the same Data Source step one needs to create all the properties that correspond how the data looks in the columns in the excel sheet to be loaded and stored into when the Data Source step is being executed.
A3.Yes, after all data has been fetched from the file.xlsx and stored in properties I use Property Transfer to distribute my data into JSON attributes for my REST request. I've tried both using Property Transfer way and assigning the data source direct for each attribute to be fetched from Data Source directly and in both ways my JSON request will automatically be populated correctly but all attributes are treated as text/string regardless if some of the attributes are numeric by definition or not.
The attached picture I had in my previous comment shows the syntax that is being auto-generated when creating property transfer, neither that way or the auto-sourcing of data as you showed as "${DataSource#attribute1" has an effect on how the attribute will look like in my JSON object. All attributes are handled as string/txt fields as I mentioned.
I think there should be a way to choose or adjust to these auto-generated syntax when assigning actions to attributes that are being transferred that allow the user to choose if this will be casting numeric or sting type of attribute.
Many thanks again for your help so far 🙂
Regards,
MFzOS
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hey @MFzOS
if you can answer my latest questions - I can then get on with sorting what you need
Cheers,
Rich
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm not quite sure I got what you meant, but if you're referring to Q3, yes I get the JSON path expression hard coded when assigning values to be transferred from properties in the data source to the attribute in the JSON object in the request.
Cheers!
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sorry i read your previous message on my phone and didnt see the answers to the questions.
Ive got my answers now. Yeah you dont need the explicit propTransfer.
Im in meetings all day, but once it ticka over to 5:30pm, i'll add in my amswer, with screenshots how i do what you want.
Cheers,
Rich
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hey @MFzOS
Im just setting up the project now to emulate what you say - are you saying your DataSource step is of type 'Excel' or is it type 'file' which then points to an Excel spreadsheet??
Cheers,
Rich
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
It is type 'Excel' where I use a spreadsheet that contains my test data.
Cheers,
