Fixed variable shadowing.

Added UTF-8 detection before making JSON.