improved save json function