1.1.0 • Published 5 years ago

json-from-text v1.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

json-from-text

NPM Version Build Status Coverage Status dependencies Status

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 for migration instructions.

Usage

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

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:

{
  "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

License

Copyright (c) 2017-2018 Tejas Shah

MIT License, see LICENSE for details.

1.1.0

5 years ago

1.0.0

6 years ago

0.1.1

6 years ago

0.1.0

7 years ago

0.0.1

7 years ago