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.
- 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.
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);
Attempts to fix a malformed JSON string by removing comments and trailing commas.
Parses a JSON string and returns the parsed JavaScript object.
Checks if a JSON string is valid.
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.
- Add support for handling nested JSON structures.
- Implement additional options for customizing JSON repair behavior.
- Enhance error handling and provide more descriptive error messages.
Special thanks to contributors who have helped improve this library.
- Email: wick@wick-studio.com
- Website: https://wickdev.xyz
- Discord: https://discord.gg/wicks
- YouTube: https://www.youtube.com/@wick_studio