Javascript にて json に変換できる文字列かどうかをチェックする

isJson とかは存在しないので、
try ~ catch を使って自作するかライブラリ使うかという感じ。

'use strict'

const body = '{"result":"success","data":[{"id":1,"name":"Simian Mobile Disco"},{"id":2,"name":"Kiasmos"}],"time":"134ms"}'

const errorBody = 'ERROR! {"result":"success","data":[{"id":1,"name":"Simian Mobile Disco"},{"id":2,"name":"Kiasmos"}],"time":"134ms"}'

let obj = null

try {
	obj = JSON.parse(errorBody)
} catch(e) {
	console.log('==========')
	console.log(e.message)
	console.log('==========')
}

console.log(obj)

try {
	obj = JSON.parse(body)
} catch(e) {
	console.log('==========')
	console.log(e.message)
	console.log('==========')
}

console.log(obj)
==========
Unexpected token E in JSON at position 0
==========
null
{ result: 'success',
  data:
   [ { id: 1, name: 'Simian Mobile Disco' },
     { id: 2, name: 'Kiasmos' } ],
  time: '134ms' }