Convert API
toJson()
Converts the passed argument to a JSON string.
- Accepts any single argument.
-
Returns a JSON string if the argument can be converted, otherwise, returns false.
If the passed argument is a valid JSON string, it will be returned as is, without conversion.
Example:
toJson({key: 'value'});
// '{"key":"value"}'
toJson('{"key":"value"}');
// '{"key":"value"}'
toJson([1, 'book', true, 'false', null]);
// '[1,"book",true,"false",null]'
toJson({});
// '{}'
toJson(10);
toJson('10');
// '10'
toJson('10 books');
// '"10 books"'