Coder Social home page Coder Social logo

bent10 / json-loose Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 150 KB

Transforms loosely structured plain object strings into valid JSON strings.

Home Page: https://www.npmjs.com/package/json-loose

License: MIT License

HTML 14.02% TypeScript 85.98%
json jsonify loose object plain-object stringify

json-loose's Introduction

json-loose

A utility to handle and transform loosely structured data into valid JSON strings. It offers an intuitive solution to ensure data consistency and compatibility, providing a safer alternative to using the Function constructor to obtain an object from a string.

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:

Type URL
ESM https://cdn.jsdelivr.net/npm/json-loose/+esm
CJS https://cdn.jsdelivr.net/npm/json-loose/dist/index.cjs
UMD https://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" }
  }
]

Related

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

json-loose's People

Contributors

bent10 avatar renovate[bot] avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

json-loose's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

github-actions
.github/workflows/pr-title.yml
  • actions/checkout v4
  • amannn/action-semantic-pull-request v5
.github/workflows/release.yml
  • actions/checkout v4
  • actions/setup-node v4
.github/workflows/validate.yml
  • actions/checkout v4
  • actions/setup-node v4
npm
package.json
  • moo ^0.5.2
  • @types/moo 0.5.9
  • doogu 3.2.13
  • vite-plugin-resolve-umd-format 1.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Fix Node.JS import

/home/d9k/.nvm/versions/node/v20.8.0/bin/node --nolazy -r ts-node/register/transpile-only parse-supabase-generated-types-with-babel.ts
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/d9k/cr/pr/examples/ts/supabase-demo/node_modules/.pnpm/[email protected]/node_modules/json-loose/dist/index.cjs.js from /home/d9k/cr/pr/examples/ts/supabase-demo/parse-supabase-generated-types-with-babel.ts not supported.
index.cjs.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename index.cjs.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/d9k/cr/pr/examples/ts/supabase-demo/node_modules/.pnpm/[email protected]/node_modules/json-loose/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.<anonymous> (/home/d9k/cr/pr/examples/ts/supabase-demo/parse-supabase-generated-types-with-babel.ts:33:38)
    at Module._compile (/home/d9k/cr/pr/examples/ts/supabase-demo/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/dist/index.js:857:29)
    at Object..ts (/home/d9k/cr/pr/examples/ts/supabase-demo/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/dist/index.js:859:16) {
  code: 'ERR_REQUIRE_ESM'
}
Process exited with code 1

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.