UNPKG

@gravityforms/utils

Version:
32 lines (30 loc) 610 B
/** * @module isJson * @description Determines if the specified string is JSON encoded. * * @since 1.0.0 * * @param {string} str The string to be tested. * * @return {boolean} Returns true if the specified string is JSON encoded. Returns false otherwise. * * @example * import { isJson } from "@gravityforms/utils"; * * function Example() { * const str = '{"name":"John", "age":30}'; * const isJsonEncoded = isJson( str ); * } * */ export default function( str ) { if ( str === null ) { return false; } try { JSON.parse( str ); } catch ( e ) { return false; } return true; }