Error handling and key sorting in formatjson