extracted functions for building tree like json structures into a separate library