# json-from-text

> An algorithm for separating embedded JSON from a string, nicely packaged into a node.js module.

Latest version **1.1.0** (published 2018-12-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-from-text
pnpm add json-from-text
yarn add json-from-text
bun add json-from-text
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2018-12-19 |
| First published | 2017-10-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 20.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | tejashah88 |
| Maintainers | tejashah88 |

## Links

- npm: https://www.npmjs.com/package/json-from-text
- Repository: https://github.com/tejashah88/json-from-text
- Homepage: https://github.com/tejashah88/json-from-text#readme
- Issues: https://github.com/tejashah88/json-from-text/issues
- npm.io page: https://npm.io/package/json-from-text

## Dependencies (2)

- [jsonic](https://npm.io/package/jsonic.md) ^0.3.1
- [xregexp](https://npm.io/package/xregexp.md) ^4.2.0

## Recent versions

- 1.1.0 (latest) — 2018-12-19
- 1.0.0 — 2018-10-08
- 0.1.1 — 2018-03-02
- 0.1.0 — 2017-10-17
- 0.0.1 — 2017-10-17

## README

# json-from-text

[![NPM Version](https://img.shields.io/npm/v/json-from-text.svg)](https://www.npmjs.com/package/json-from-text)
[![Build Status](https://travis-ci.org/tejashah88/json-from-text.svg?branch=master)](https://travis-ci.org/tejashah88/json-from-text)
[![Coverage Status](https://coveralls.io/repos/github/tejashah88/json-from-text/badge.svg)](https://coveralls.io/github/tejashah88/json-from-text)
[![dependencies Status](https://david-dm.org/tejashah88/json-from-text/status.svg)](https://david-dm.org/tejashah88/json-from-text)

An algorithm for separating embedded JSON from a string, nicely packaged into an NPM module. It will attempt to parse potential JSON strings into objects and leave them as-is if it fails.

## Upgrading from < 1.0
If you are upgrading from < 1.0, please see [UPGRADING](UPGRADING.md) for migration instructions.

## Usage
```javascript
Object jsonFromText(String mixedStr, Boolean parseToObject)
```

### Parameters:
* `mixedStr`: The string to separate the JSON from the text.
* `parseToObject`: If set to `false`, the resulting JSON strings will not be translated to JSON objects. Default is `true`.

## Example
### Input
```javascript
var jsonFromText = require('json-from-text');
var sampleText = "There was a change from {'animal':'dog', 'color':'blue'} to {'animal':'cat', 'color':'red'}'";
var results = jsonFromText(sampleText);
console.log(JSON.stringify(results, null, 2));
```

### Output:
```json
{
  "textResults": [
    "There was a change from '",
    "' to '",
    "'"
  ],
  "jsonResults": [
    {
      "animal": "dog",
      "color": "blue"
    },
    {
      "animal": "cat",
      "color": "red"
    }
  ],
  "fullResults": [
    {
      "type": "text",
      "value": "There was a change from '"
    },
    {
      "type": "json",
      "parsed": true,
      "value": {
        "animal": "dog",
        "color": "blue"
      }
    },
    {
      "type": "text",
      "value": "' to '"
    },
    {
      "type": "json",
      "parsed": true,
      "value": {
        "animal": "cat",
        "color": "red"
      }
    },
    {
      "type": "text",
      "value": "'"
    }
  ]
}
```

## Mentions
* [rjrodger](https://github.com/rjrodger) for his [jsonic](https://github.com/rjrodger/jsonic) module, which does lenient JSON parsing

## License
Copyright (c) 2017-2018 Tejas Shah

MIT License, see [LICENSE](LICENSE.md) for details.

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