cancel
Showing results for 
Search instead for 
Did you mean: 

JSON as dataSource

SOLVED
Highlighted
Contributor

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

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Community Hero

Re: JSON as dataSource

Can you please tell what have you tried? So, every one will understand what does not work.
So, you need two values per row and it will be of 6 rows which you want to loop thru. Correct?
Hope below link can help you.
https://support.smartbear.com/readyapi/docs/testing/data-driven/types/groovy.html


Regards,
Rao.

View solution in original post

Highlighted
Community Hero

Re: JSON as dataSource

@mpartyka 

Try changing below statement

 

if (row+1 < data.size)

 

To

if (row < data.size)


Regards,
Rao.

View solution in original post

8 REPLIES 8
Highlighted
Community Hero

Re: JSON as dataSource

Can you please tell what have you tried? So, every one will understand what does not work.
So, you need two values per row and it will be of 6 rows which you want to loop thru. Correct?
Hope below link can help you.
https://support.smartbear.com/readyapi/docs/testing/data-driven/types/groovy.html


Regards,
Rao.

View solution in original post

Highlighted
Community Hero

Re: JSON as dataSource

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



Regards,
Rao.
Highlighted
Community Manager

Re: JSON as dataSource

Thanks Rao.

 

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


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Highlighted
Contributor

Re: JSON as dataSource

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]
}
Highlighted
Community Hero

Re: JSON as dataSource

@mpartyka 

 

Thanks for trying and implementing your case.

I couldn't test this, would you please give it a try?

 

def str = context.expand( '${DataSource-readJSON#JSONstring}' )

// Get current row
def row = testRunner.testCase.testSteps["GroovyDataSource"].currentRow

log.info "row = $row"

// 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() }
if (row+1 < data.size) {
	result["table"] = data[row].table
	result["column"] = data[row].column
}


Regards,
Rao.
Highlighted
Contributor

Re: JSON as dataSource

 Thanks again Rao ... I implemented your suggestion above ... almost there.  Missing ROW 6 .   

 

ROW 1 - ProcedureTable1, columnAA
ROW 2 - ProcedureTable1, columnBB
ROW 3 - ProcedureTable1, columnCC
ROW 4 - ProcedureTable2, columnAA
ROW 5 - ProcedureTable2, columnBB
ROW 6 - ProcedureTable3, columnAA   - this row is not processed

 

By reviewing the script log ... i can see that 

 

  • The data loop starts executing the "GroovyDataSource" test step for row = 5 (see script log in screen capture below - "row = 5" is present in the script log);
  •  
  • The "GroovyScript1" test step logs the table name & column name from the "GroovyDataSource" test step.  This log info does NOT appear in the script log below for row 5.  

 

Picture1.png

 

Thanks again for all your help.   

Highlighted
Community Hero

Re: JSON as dataSource

@mpartyka 

Try changing below statement

 

if (row+1 < data.size)

 

To

if (row < data.size)


Regards,
Rao.

View solution in original post

Highlighted
Contributor

Re: JSON as dataSource

Thanks so much Rao - it works perfectly now!   That was it ... should have caught that myself :smileyembarrassed:

 

New Here?
Join us and watch the welcome video:
Top Kudoed Authors