Implement toJSON function add remove inline json object generation
Refactored code