refactoring to remove duplication and hard dependency on newtonsoft