Convert to JSON

Works just as well in node and makes it more flexible for use in other contexts.