exportToJSON function

Export the given object to a JSON object, ready to be written to files.