Fix encoding issues in nested output

This fixes #15542