Socket
Socket
Sign inDemoInstall

extract-json-from-string

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    extract-json-from-string

Extract JSON/javascript objects from strings


Version published
Maintainers
1
Install size
27.3 kB
Created

Readme

Source

Build Status downloads npm Maintainability Test Coverage dependencies Size

extract-json-from-string

Extract JSON/javascript objects from strings

Installation

npm install --save extract-json-from-string

Summary

Extract random JSON and javascript objects from a longer string, e.g. "Expected { foo: 'bar' } to equal { foo: 'baz' }" (I'm looking at you jasmine 1.3). Also works with arrays.

Usage

Just pass the string into the one exported function and get a list of objects and arrays contained therein returned to you. If the string contains no valid objects or arrays (valid objects or arrays), you'll get an empty array back.

Node

const extract = require('extract-json-from-string');

let objects = extract('Expected { foo: "bar" } to equal { foo: "baz" }');
// [
//   { foo: 'bar' },
//   { foo: 'baz' }
// ]

Browser

let objects = window.extractJson('Expected { foo: "bar" } to equal { foo: "baz" }');
// [
//   { foo: 'bar' },
//   { foo: 'baz' }
// ]

N.B.

For the time being, I've written a very naive implementation. There are lots of ways to break this (like stringified JSON or escaped quotes within the value of a property). Please report any issues, and I'll do my best to fix them and make it less naive.

Contributing

Please see the contribution guidelines.

Keywords

FAQs

Last updated on 02 Jan 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc