json-excise
v1.0.0
Published
Slice valid JSON out of larger strings, given the start index.
Readme
json-excise
Slice valid JSON out of larger strings, given the start index.
Useful for reading it out of other documents like HTML pages without a full parser.
More formally:
Given an index S into a string, finds the minimal index E with E > S such that slice(string, S, E) is valid JSON. Returns -1 if there is no such index.
Installing
npm install json-excise
Alternatively, the whole library consists of a single file, json-excise.ts, which contains its license and usage. You can copy-paste it into your library.
Sample usage
import { getJsonEnd } from 'json-excise';
let input = `some data: { "foo": [1, 2, 3] }`;
let marker = '"foo":';
let start = input.indexOf(marker) + marker.length;
let end = getJsonEnd(input, start);
let parsed = JSON.parse(input.slice(start, end));
console.log(parsed); // [1, 2, 3]