diff --git a/src/index.ts b/src/index.ts index 8282cbc..086a21d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,7 +22,11 @@ const parse = (data: string): T[] => { if (char === '}') { depth-- - if (depth === 0) results.push(JSON.parse(currentJson)) + if (depth === 0) { + try { + results.push(JSON.parse(currentJson)) + } catch (error) {} + } } } diff --git a/test/index.test.ts b/test/index.test.ts index c71dd6e..fd4f16e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -34,3 +34,10 @@ it('should correctly parse a JSON string with objects separated by arbitrary tex expect(value).toEqual([{ foo: 'bar' }, { abc: 'xyz' }, { def: 'ghi' }]) }) + +it('should not throw an error when parsing invalid JSON', () => { + const json = '{ "foo": invalid } { "abc": "xyz" }' + const value = JsonExtractify.parse(json) + + expect(value).toEqual([{ abc: 'xyz' }]) +})