You should JSON encode the entire output, instead of outputting the json encoded version of each row:
$output = array();
//$rows is another query
foreach($rows as $row) {
$sql = 'SELECT field1, field2 FROM table WHERE field1= :field';
$stmt = $db->prepare($sql);
$stmt->bindValue(':field', trim($row['field_1']));
$stmt->execute();
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
$output[] = $array;
}
echo json_encode($output);
Answering your question, to work with your JSON in JavaScript, you treat it as if it were an array of objects. You can even use jQuery to help loop through the results for you with $.each:
if(response.length != 0) {
$.each(response, function(index, row) {
console.log(row);
// Access your row variables like so:
// row.field1, row.field2, row.field3, etc.
}
}
If you prefer natively looping through, you can do the following:
// Let i start at zero. If the response array length is less than i, execute the block, then increment i by 1.
for(var i = 0; response.length < i; i += 1) {
}
Related Question / Further Reading: How to parse JSON in JavaScript