Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
json-restructure
Advanced tools
Readme
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.
To install JSON Restructure, you can use npm :
npm install json-restructure
JSON Restructure provides several functions to handle JSON strings :
const { fixJson } = require('json-restructure');
const malformedJson = '{"name":"John","age":30,}//Trailing comma';
const fixedJson = fixJson(malformedJson);
console.log('Fixed JSON:', fixedJson);
const { parseJson } = require('json-restructure');
const jsonString = '{"name":"John","age":30}';
const parsedJson = parseJson(jsonString);
console.log('Parsed JSON:', parsedJson);
const { validateJson } = require('json-restructure');
const jsonString = '{"name":"John","age":30}';
const isValid = validateJson(jsonString);
console.log('Is valid JSON?', isValid);
const { repairJson } = require('json-restructure');
const malformedJson = '{"name":"John","age":30,}//Trailing comma';
const repairedJson = repairJson(malformedJson);
console.log('Repaired JSON:', repairedJson);
// 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);
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.
To run the tests for JSON Restructure, use the following command:
npm test
Contributions are welcome! Please read the contribution guidelines before submitting pull requests.
This project is licensed under the MIT License.
If you encounter any issues or have any feedback, please don't hesitate to open an issue on GitHub.
Special thanks to contributors who have helped improve this library.
FAQs
A TypeScript library for repairing malformed JSON strings
The npm package json-restructure receives a total of 96 weekly downloads. As such, json-restructure popularity was classified as not popular.
We found that json-restructure demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.