Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-loose

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-loose

Transforms loosely structured plain object strings into valid JSON strings

  • 1.2.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
635
decreased by-31.2%
Maintainers
0
Weekly downloads
 
Created
Source

json-loose

A utility that converts loosely structured data into valid JSON strings, ensuring data consistency and compatibility. It provides a safer alternative to using the Function constructor to create objects from strings.

Install

You can install this module using npm or yarn, it's only 2.93 kB │ gzip: 1.32 kB:

npm i json-loose
# or
yarn add json-loose

Alternatively, you can also include this module directly in your HTML file from CDN:

TypeURL
ESMhttps://cdn.jsdelivr.net/npm/json-loose/+esm
CJShttps://cdn.jsdelivr.net/npm/json-loose/dist/index.cjs
UMDhttps://cdn.jsdelivr.net/npm/json-loose/dist/index.umd.js

Usage

The jsonLoose function takes an invalid JSON string as input and returns a JSON-like string representation of the transformed data.

import jsonLoose from 'json-loose'

const invalidJSON = `
{
  name: 'Bambang Ekalaya',
  username: "@palgunadi",
  age: 30,
  isStudent: true
}
`
const data = jsonLoose(invalidJSON)
// now you can `JSON.parse(data)`

console.log(data)

Yields:

{
  "name": "Bambang Ekalaya",
  "username": "@palgunadi",
  "age": 30,
  "isStudent": true
}

You can also specify an optional context object to transform Identifier values:

import jsonLoose from 'json-loose'

const invalidJSON = `
[
  "foo",
  true,
  [1, 2, wife],
  {
    [n]: 'Bambang Ekalaya',
    username: "@palgunadi",
    age: 30,
    address: [{city:city}, [country]],
    skills: skills,
    isStudent: true,
    relation: {
      wife: wife,
      guru: guru,
      [bar]: "qux"
    },
  },
],
`

const context = {
  n: 'name',
  skills: 'Archery',
  city: 'Nishada',
  country: 'Aravalli',
  wife: 'Anggraini',
  guru: 'Drona'
}

const data = jsonLoose(invalidJSON, context)
// now you can `JSON.parse(data)`

console.log(data)

Yields:

[
  "foo",
  true,
  [1, 2, "Anggraini"],
  {
    "name": "Bambang Ekalaya",
    "username": "@palgunadi",
    "age": 30,
    "address": [{ "city": "Nishada" }, ["Aravalli"]],
    "skills": "Archery",
    "isStudent": true,
    "relation": { "wife": "Anggraini", "guru": "Drona", "bar": "qux" }
  }
]
  • attributes-parser – A utility for parsing and tokenizing attributes string into meaningful tokens and key-value pairs.

Contributing

We 💛  issues.

When committing, please conform to the semantic-release commit standards. Please install commitizen and the adapter globally, if you have not already.

npm i -g commitizen cz-conventional-changelog

Now you can use git cz or just cz instead of git commit when committing. You can also use git-cz, which is an alias for cz.

git add . && git cz

License

GitHub

A project by Stilearning © 2023.

Keywords

FAQs

Package last updated on 27 Aug 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc