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

json-restructure

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-restructure

A TypeScript library for repairing malformed JSON strings

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
38
increased by442.86%
Maintainers
1
Weekly downloads
 
Created
Source

JSON Restructure

GitHub license GitHub issues GitHub stars

JSON Restructure is a TypeScript library made by Wick Studio to help developers repair malformed JSON strings easily. It provides functions to fix, parse, validate, and repair JSON strings, making it simpler to work with JSON data in your projects.

Features

  • Fix Malformed JSON : JSON Restructure can fix JSON strings with syntax errors, such as missing or trailing commas, comments, etc.
  • Parse JSON : It allows you to parse JSON strings and convert them into JavaScript objects.
  • Validate JSON : You can validate JSON strings to check if they are well-formed.
  • Repair JSON : JSON Restructure attempts to repair JSON strings that cannot be parsed due to syntax errors.

Installation

To install JSON Restructure, you can use npm :

npm install json-restructure

Usage

JSON Restructure provides several functions to handle JSON strings :

Fixing Malformed JSON

const { fixJson } = require('json-restructure');

const malformedJson = '{"name":"John","age":30,}//Trailing comma';
const fixedJson = fixJson(malformedJson);
console.log('Fixed JSON:', fixedJson);

Parsing JSON

const { parseJson } = require('json-restructure');

const jsonString = '{"name":"John","age":30}';
const parsedJson = parseJson(jsonString);
console.log('Parsed JSON:', parsedJson);

Validating JSON

const { validateJson } = require('json-restructure');

const jsonString = '{"name":"John","age":30}';
const isValid = validateJson(jsonString);
console.log('Is valid JSON?', isValid);

Repairing Malformed JSON

const { repairJson } = require('json-restructure');

const malformedJson = '{"name":"John","age":30,}//Trailing comma';
const repairedJson = repairJson(malformedJson);
console.log('Repaired JSON:', repairedJson);

Full Usage

// Import the functions from the library
const { fixJson, parseJson, validateJson, repairJson } = require('json-restructure');

// Example usage
const malformedJson = '{"name":"John","age":30,}//Trailing comma';
const fixedJson = fixJson(malformedJson);
console.log('Fixed JSON:', fixedJson);

// Parse JSON
try {
  const parsedJson = parseJson(fixedJson);
  console.log('Parsed JSON:', parsedJson);
} catch (error) {
  console.error('Error parsing JSON:', error.message);
}

// Validate JSON
const isValid = validateJson(fixedJson);
console.log('Is valid JSON?', isValid);

// Repair JSON
const repairedJson = repairJson(malformedJson);
console.log('Repaired JSON:', repairedJson);

API Documentation

fixJson(jsonString: string): string

Attempts to fix a malformed JSON string by removing comments and trailing commas.

parseJson(jsonString: string): any

Parses a JSON string and returns the parsed JavaScript object.

validateJson(jsonString: string): boolean

Checks if a JSON string is valid.

repairJson(jsonString: string): string

Attempts to repair a malformed JSON string.

Running Tests

To run the tests for JSON Restructure, use the following command:

npm test

Contributing

Contributions are welcome! Please read the contribution guidelines before submitting pull requests.

License

This project is licensed under the MIT License.

Issues and Feedback

If you encounter any issues or have any feedback, please don't hesitate to open an issue on GitHub.

Roadmap

  • Add support for handling nested JSON structures.
  • Implement additional options for customizing JSON repair behavior.
  • Enhance error handling and provide more descriptive error messages.

Acknowledgments

Special thanks to contributors who have helped improve this library.

Contact

Keywords

FAQs

Package last updated on 10 Apr 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