Giter Club home page Giter Club logo

json-from-text's Introduction

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.

json-from-text's People

Contributors

dhanush123 avatar tejashah88 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

dhanush123

json-from-text's Issues

Add testing

  • Add unit tests
  • Add Travis CI integration
  • Add coveralls.io integration

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.