# extract-first-json

> Extract the first JSON object or array from a string

Latest version **2.0.2** (published 2023-01-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install extract-first-json
pnpm add extract-first-json
yarn add extract-first-json
bun add extract-first-json
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2023-01-30 |
| First published | 2020-09-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | ^14.13.1 \|\| >=16.0.0 |
| Dependencies | 2 |
| Unpacked size | 6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Connor White |
| Maintainers | bconnorwhite |
| Keywords | extract, json, object, array, from, string |

## Links

- npm: https://www.npmjs.com/package/extract-first-json
- Repository: https://github.com/bconnorwhite/extract-first-json
- Homepage: https://connorwhite.dev/github/extract-first-json
- Issues: https://github.com/bconnorwhite/extract-first-json/issues
- npm.io page: https://npm.io/package/extract-first-json

## Dependencies (2)

- [reduce-first](https://npm.io/package/reduce-first.md) ^2.0.0
- [parse-json-object](https://npm.io/package/parse-json-object.md) ^3.0.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.2 (latest) — 2023-01-30
- 2.0.1 — 2023-01-30
- 2.0.0 — 2023-01-30
- 1.0.1 — 2020-09-20
- 1.0.0 — 2020-09-20

## README

<!--BEGIN HEADER-->
<div id="top" align="center">
  <h1>extract-first-json</h1>
  <a href="https://npmjs.com/package/extract-first-json">
    <img alt="NPM" src="https://img.shields.io/npm/v/extract-first-json.svg">
  </a>
  <a href="https://github.com/bconnorwhite/extract-first-json">
    <img alt="TypeScript" src="https://img.shields.io/github/languages/top/bconnorwhite/extract-first-json.svg">
  </a>
</div>

<br />

<blockquote align="center">Extract the first JSON object or array from a string.</blockquote>

<br />

_If I should maintain this repo, please ⭐️_
<a href="https://github.com/bconnorwhite/extract-first-json">
  <img align="right" alt="GitHub stars" src="https://img.shields.io/github/stars/bconnorwhite/extract-first-json?label=%E2%AD%90%EF%B8%8F&style=social">
</a>

_DM me on [Twitter](https://twitter.com/bconnorwhite) if you have questions or suggestions._
<a href="https://twitter.com/bconnorwhite">
  <img align="right" alt="Twitter" src="https://img.shields.io/twitter/url?label=%40bconnorwhite&style=social&url=https%3A%2F%2Ftwitter.com%2Fbconnorwhite">
</a>

---
<!--END HEADER-->

## Installation

```sh
yarn add extract-first-json
```

```sh
npm install extract-first-json
```

```sh
pnpm add extract-first-json
```

<br />

## Usage

```ts
import { extractJSON, extractJSONObject, extractJSONArray } from "extract-first-json";

const string = `Example: { "ok": true }`;

const arrayString = `Example: [{ "ok": true }]`;

extractJSON(string); // { ok: true }

extractJSONObject(string); // { ok: true }

extractJSONArray(arrayString); // [{ "ok": true }]
```

<!--BEGIN FOOTER-->

<br />

<h2 id="dependencies">Dependencies<a href="https://www.npmjs.com/package/extract-first-json?activeTab=dependencies"><img align="right" alt="dependencies" src="https://img.shields.io/librariesio/release/npm/extract-first-json.svg"></a></h2>

- [parse-json-object](https://www.npmjs.com/package/parse-json-object): Parse a typed JSON object from a string
- [reduce-first](https://www.npmjs.com/package/reduce-first): Return early from reduce

<br />

<h3>Dev Dependencies</h3>

- [autorepo](https://www.npmjs.com/package/autorepo): Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.

<br />

<h2 id="license">License <a href="https://opensource.org/licenses/MIT"><img align="right" alt="license" src="https://img.shields.io/npm/l/extract-first-json.svg"></a></h2>

[MIT](https://opensource.org/licenses/MIT)
<!--END FOOTER-->

---
_Source: https://npm.io/package/extract-first-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
