How are you outputting the json? That seems like something you could handle on the output page that reads in the json. What format do you want the json to have?
I think you would probably have to hand code a loop through the first recordset results so you can get the corresponding rows and then add it to the array so you can json encode it if you wanted to do it on this page.