Forum Discussion

mpartyka's avatar
mpartyka
Contributor
5 years ago

JSON as dataSource

I am trying to read a JSON file in as a DataSource test step (see JSON below).  The number of nodes of the properties (named "columns") varies with each tableName in the JSON file.  Is it possible to read in row by row and end up with the simple result posted below?   It seems like this should be simple, but I've been struggling for hours!  Thanks!

 

INPUT JSON FILE

{
	"ontologyName": "Procedure",
	"linkedResources": [{
			"tableName": "ProcedureTable1",
			"columns": [
				"columnAA",
				"columnBB",
				"columnCC"
			]
		},
		{
			"tableName": "ProcedureTable2",
			"columns": [
				"columnAA",
				"columnBB"
			]
		},
		{
			"tableName": "ProcedureTable3",
			"columns": [
				"columnAA"
			]
		}
	]
}

DESIRED OUTPUT

ProcedureTable1,columnAA
ProcedureTable1,columnBB
ProcedureTable1,columnCC
ProcedureTable2,columnAA
ProcedureTable2,columnBB
ProcedureTable3,columnAA

 

  • nmrao's avatar
    nmrao
    Champion Level 3
    def str = '''{
        "ontologyName": "Procedure",
        "linkedResources": [{
                "tableName": "ProcedureTable1",
                "columns": [
                    "columnAA",
                    "columnBB",
                    "columnCC"
                ]
            },
            {
                "tableName": "ProcedureTable2",
                "columns": [
                    "columnAA",
                    "columnBB"
                ]
            },
            {
                "tableName": "ProcedureTable3",
                "columns": [
                    "columnAA"
                ]
            }
        ]
    }'''
    
    def json = new groovy.json.JsonSlurper().parseText(str)
    def data = json.linkedResources.inject([]) { list, table -> list << table.columns.collect { ['table': table.tableName, 'column': it]}; list.flatten() }
    data.each {
      log.info "${it.table}, ${it.column}"
    }

    Is this what you needed?

     

    You can test this online

    https://ideone.com/NkcNMd

     

    If you want to implement it in your case, you need make few modifications to the above script.

    In the Data source step, use Groovy type. And have above script in place

     

    1. instead of fixed data, you can use json file and read that and assign it to str variable

    2. need to define a variable and assign current row count

    3. loop thru variable data and assign it variable result

     

    In the test request step, where the data is required use table, column respectively.

    Refer documetation link.

    https://support.smartbear.com/readyapi/docs/testing/data-driven/types/groovy.html

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      Thanks Rao.

       

      Hi mpartyka , did you have a chance to try the suggestion?

      • mpartyka's avatar
        mpartyka
        Contributor

        This is awesome ... exactly what i was looking for to loop through and read the JSON.  

         

        I am now working on this step "2. need to define a variable and assign current row count" and am having trouble with getting the data source test step to read only a single row of the output at a time ... the step is reading all 6 rows in a single pass (of data loop) instead of only 1 row.   I am also having an issue getting the test step to save the property.    Can you help ?  Thanks

         

        ROW 1 - ProcedureTable1, columnAA
        ROW 2 - ProcedureTable1, columnBB
        ROW 3 - ProcedureTable1, columnCC
        ROW 4 - ProcedureTable2, columnAA
        ROW 5 - ProcedureTable2, columnBB
        ROW 6 - ProcedureTable3, columnAA

         

         

        here is groovy data source i am currently trying to get to work ....  Thanks for any help !!!

        def str = context.expand( '${DataSource-readJSON#JSONstring}' )
        
        // Get current row
        def row = testRunner.testCase.testSteps["GroovyDataSource"].currentRow;
        
        log.info ("row = " + row)
        
        def grid = [];
        
        // Get a list of items in the grid array
        
        def json = new groovy.json.JsonSlurper().parseText(str)
        def data = json.linkedResources.inject([]) { list, table -> list << table.columns.collect { ['table': table.tableName, 'column': it]}; list.flatten() }
        data.each {
          log.info "${it.table}, ${it.column}"
        }
        
        // ***********************************
        //{
         //   // Add the name of the file to the list
        //    data -> grid.add(it.table,it.column);
        //}
        // ***********************************
        
        if (row < grid.size) {
            // Return the name to data source's "File" property
        
        
        result["table"] = it.table[row]
        result["column"] = it.column[row]
        }