Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/imoxto/extract-json-from-text
https://github.com/imoxto/extract-json-from-text
Last synced: 9 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/imoxto/extract-json-from-text
- Owner: imoxto
- License: mit
- Created: 2023-06-01T15:07:45.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-06-15T11:54:33.000Z (over 1 year ago)
- Last Synced: 2024-10-12T11:10:54.740Z (2 months ago)
- Language: JavaScript
- Size: 80.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Extract Json From Text
Extract a JSON oboject or array from any piece of text
## Usage
### Install
```bash
npm i extract-json-from-text
```### Import
```js
const { jsonFromText } = require("extract-json-from-text");
``````ts
import { jsonFromText } from "extract-json-from-text";
```### Object Extraction
By default, it looks for an object in the string text
```ts
const str = `Lorem ipsum dolor sit amet, { "hello": "World" } consectetur adipiscing elit.`;
const jsonResults = jsonFromText(str);
console.log(jsonResults); // Object { "hello": "World" }
```### Array
If you have to extract an array from the text
```ts
const str = `Lorem ipsum dolor sit amet, [ "hello", "World" ] consectetur adipiscing elit.`;
const jsonResults = jsonFromText(str, "array");
console.log(jsonResults); // Array [ "hello", "World" ]
```