Moved json formatting to its own function and improved json formatting.