Fix potential invalid JSON output in MetadataWriter (#14591)

Change-Id: I90ec428ae11727b9762d53ebb8d94f8c850c425b