Fix encoding issues in nested output

This fixes #15542
Merge pull request #15546 from makinacorpus/b2

Fix encoding issues in nested output