code refactoring and add test on valid json and invalid json