JSON Restructure
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
const { fixJson, parseJson, validateJson, repairJson } = require('json-restructure');
const malformedJson = '{"name":"John","age":30,}//Trailing comma';
const fixedJson = fixJson(malformedJson);
console.log('Fixed JSON:', fixedJson);
try {
const parsedJson = parseJson(fixedJson);
console.log('Parsed JSON:', parsedJson);
} catch (error) {
console.error('Error parsing JSON:', error.message);
}
const isValid = validateJson(fixedJson);
console.log('Is valid JSON?', isValid);
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