Extract a JSON oboject or array from any piece of text
npm i extract-json-from-text
const { jsonFromText } = require("extract-json-from-text");
import { jsonFromText } from "extract-json-from-text";
By default, it looks for an object in the string text
const str = `Lorem ipsum dolor sit amet, { "hello": "World" } consectetur adipiscing elit.`;
const jsonResults = jsonFromText(str);
console.log(jsonResults); // Object { "hello": "World" }
If you have to extract an array from the text
const str = `Lorem ipsum dolor sit amet, [ "hello", "World" ] consectetur adipiscing elit.`;
const jsonResults = jsonFromText(str, "array");
console.log(jsonResults); // Array [ "hello", "World" ]