Twig will render any given variable as html encoded. However, as you want to json encode the result, you need need to iterate through the data yourself as Twig doesn't delve into the array for you.