Forum Discussion

SahajP's avatar
SahajP
Occasional Contributor
7 years ago

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.

  • 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

  • richie's avatar
    richie
    Community Hero

    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

    • SahajP's avatar
      SahajP
      Occasional Contributor

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

    • SahajP's avatar
      SahajP
      Occasional Contributor

      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.

      • richie's avatar
        richie
        Community Hero

        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