cancel
Showing results for 
Search instead for 
Did you mean: 

How to extract JSON data from external file as Datasource via Ready API?

SOLVED
Highlighted
Occasional Contributor

How to extract JSON data from external file as Datasource via Ready API?

I want to get data via JSON external file as datasource. So can someone help me here how can i get it?

Is there any possibility that we can put JSON script in excel cell & extract via Ready API & use further for any requests from Datasource? If not possible then please help me any similar way to use JSON file as Datasource. Please let me know if anymore details require.

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: How to extract JSON data from external file as Datasource via Ready API?

Hi,

 

 

It's probably possible to grab .json from individual cells/rows in a spreadsheet but why would you want to?

 

I have lots of tests where I have individual test data saved in .json files (each file corresponds to a test/request) - I use the 'directory' datasource option. 

 

This allows you to pass the whole file contents to a single property to then POST the filecontents via the property in a subsequent SOAP/REST request.

 

This option enables me to loop through multiple files (multiple files in a single directory) or just a single file in a single directory for a non-looped execution run.

 

So  a simple test case hierarchy is as follows:

 

Datasource (directorytype) - fileContents property created (multiple .json files in directory)
POST (${DataSource#fileContents}) <-- property specified in request body sources the fileContents property
DatasourceLoop (loops back round to the next file in the directory - repeats until all files in directory have been processed

You can also paste the .json content into the body of your POST/PUT/PATCH requests  - however this doesn't lend itself to looping test cases and also I find makes updating the testdata more difficult.

 

Sure the other users will have lots of other options for this - but this tends to be my goto approach.  

 

Cheers,

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta

View solution in original post

7 REPLIES 7
Highlighted
Community Hero

Re: How to extract JSON data from external file as Datasource via Ready API?

Hi,

 

 

It's probably possible to grab .json from individual cells/rows in a spreadsheet but why would you want to?

 

I have lots of tests where I have individual test data saved in .json files (each file corresponds to a test/request) - I use the 'directory' datasource option. 

 

This allows you to pass the whole file contents to a single property to then POST the filecontents via the property in a subsequent SOAP/REST request.

 

This option enables me to loop through multiple files (multiple files in a single directory) or just a single file in a single directory for a non-looped execution run.

 

So  a simple test case hierarchy is as follows:

 

Datasource (directorytype) - fileContents property created (multiple .json files in directory)
POST (${DataSource#fileContents}) <-- property specified in request body sources the fileContents property
DatasourceLoop (loops back round to the next file in the directory - repeats until all files in directory have been processed

You can also paste the .json content into the body of your POST/PUT/PATCH requests  - however this doesn't lend itself to looping test cases and also I find makes updating the testdata more difficult.

 

Sure the other users will have lots of other options for this - but this tends to be my goto approach.  

 

Cheers,

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta

View solution in original post

Highlighted
Occasional Contributor

Re: How to extract JSON data from external file as Datasource via Ready API?

Thanks Richie for the solution! It worked for my cases. Now I'm extracting data with Directory.

Highlighted
Occasional Contributor

Re: How to extract JSON data from external file as Datasource via Ready API?

By this solution I'm able to get all JSON files via Directory datasource, but here I'm facing issue that is,

- Via Get Data option from Request, not able to select single JSON file from Datasource directory, it just shows '${#[TestSuite#TestCase#DataSource]#FileContent}'. Is there any way to select any single JSON from all JSON files which we got from directory?

 

- Here is my scenario, 5 Post requests & for each I'm getting JSON data from directory via data source & 5 test cases are created, so want to use those JSON data in respective 5 Post requests. Currently I'm validating by adding datasource in each test case to get JSON & calling same JSON via Get Data option at request.

 

So please let me know if here any solution on my points.

 

Thanks in advance.

Highlighted
Community Hero

Re: How to extract JSON data from external file as Datasource via Ready API?

Hi,

 

I'm sorry - I might not be understanding all of the question correctly - so I'm going to just describe and hope I answer your question.

 

Using the datasource (Directory) option (when you have multiple files in the directory) and the previous approach discussed will just (starting from the first file) grab the file and post the filecontents via the fileContents property created on the directory datasource, and move onto the next file (as long as there are multiple files in the directory and you have a datasourceloop step).....there's no way I know of to grab a specific file in the directory

 

Including multiple files in the datasource allows you to use the datasourceloop step.  If you have only a single file in the directory - the test will run once and using this approach you know which file you have picked up (because there's only 1 in the directory).

Using the datasourceloop step only works if the assertions on the REST/SOAP test steps in your test case are identical for each of the test data files you have in your directory

 

I sometimes have multiple files in my datasource (directory type) and then I include a datasourceloop step at the end to move onto the next file in the directory to repeat until all files are processed.

Othertimes I have a single file in my datasource (directory type) and then I don't include a datasourceloop step because there's no point.

 

From your post you state "- Here is my scenario, 5 Post requests & for each I'm getting JSON data from directory via data source & 5 test cases are created, so want to use those JSON data in respective 5 Post requests. Currently I'm validating by adding datasource in each test case to get JSON & calling same JSON via Get Data option at request."

 

You state you have 5 ,json test data files, 5 posts, and 5 test cases.  If you have 1 testcase rather than 5 - you would have multiple files in your datasource and you would include the datasourceloop step in the testcase.  If you are stating you have 5 test cases, you wouldn't include multiple files in the datasource.

 

I'm struggling a bit here to see the problem - please see the possibly testsuite hierarchies you have a choice on below:

 

TestSuite1

TestCase1
Datasource (directory) - 1 file (fileContents property)
POST Request (sourcing ${Datasource#fileContents})

TestCase2
Datasource (directory) - 1 file (fileContents property)
POST Request (sourcing ${Datasource#fileContents})

TestCase3
Datasource (directory) - 1 file (fileContents property)
POST Request (sourcing ${Datasource#fileContents})

TestCase4
Datasource (directory) - 1 file (fileContents property)
POST Request (sourcing ${Datasource#fileContents})

TestCase5
Datasource (directory) - 1 file (fileContents property)
POST Request (sourcing ${Datasource#fileContents})

 

OR

 

TestSuite1

TestCase1
Datasource (directory) - 5 files (fileContents property)
POST Request (sourcing ${Datasource#fileContents})
DatasourceLoop

 

As I state above - the option of having multiple files in your datasource means you can loop and means you only have 1 test case.  This is handy - but will only work if the assertions you create on  your POST request are valid for all 5 runs for your tests.

 

I don't know whether I've helped at all or just rambled on.

 

Does that help? Or if not - can you try and  be a little more descriptive in your question?

 

Cheers,

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Hero

Re: How to extract JSON data from external file as Datasource via Ready API?

ive just reread your post. are you saying you have 5 different post requests within a single test case? Im assuming as you say you have 5 POSTs they are different requests? or are you saying you have a single POST request and you are using the 5 .json test data files for the body of that single POST request?

from your description i read it like you have 5 different POST requests in which case id have my testdata files in different directories.

confused!
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Occasional Contributor

Re: How to extract JSON data from external file as Datasource via Ready API?

Thanks Richie for quick response.

 

On your first reply your understanding is correct.

In my test cases there are 5 different POST requests & each has 1 JSON file. I got your solution via data source loop, but I need to handle my test cases as independent instead of 5 cases in one test case. So here I'm not able to use data source loop as data source loop works within the one test case so.

Highlighted
Community Hero

Re: How to extract JSON data from external file as Datasource via Ready API?

Hi,

 

I'm still struggling to understand I'm afraid but I think

 

1. do you mean that you have 5 different POST requests, and each of those 5 different POST requests uses a different .json test data file.

 

2.  For a specific POST request - are you going to need to submit this multiple times - i.e. will you have multiple test data files for each POST request - or just a single file.

 

There's no reason you cannot include >1 datasource within your test case - (having 5 datasource steps providing the data for each of the 5 POST request steps) 

 

 

HOWEVER - I think this is a slow/cumbersome approach that could be handled more simply.  You could go back to the datasource spreadsheet approach (which I haven't done myself) or the datasource 'Grid' option (which I have done before)

 

I'm sorry I'm struggling to provide a clear, concise answer - I'm just not quite understanding what you need

 

 

thanks,

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors