loose-json-go

A flexible Go library for parsing malformed or "loose" JSON data, similar to Python's dirty-json. This library can handle various JSON formatting issues and convert them to valid JSON.
Features
✅ Trailing Commas - Handles trailing commas in objects and arrays
✅ Unquoted Keys - Supports unquoted object keys
✅ Single Quotes - Accepts single quotes instead of double quotes
✅ Comments - Removes single-line (//) and multi-line (/* */) comments
✅ Missing Brackets - Auto-closes missing brackets and braces
✅ Hex Numbers - Converts hexadecimal numbers (e.g., 0xFF)
✅ Special Values - Handles NaN, Infinity, undefined
✅ Multiple Extra Commas - Tolerates multiple consecutive commas
✅ Mixed Quotes - Handles mixed single and double quotes
✅ Scientific Notation - Supports numbers in scientific notation
Installation
go get github.com/nightosong/loose-json-go
Quick Start
package main
import (
"fmt"
"log"
loosejson "github.com/nightosong/loose-json-go"
)
func main() {
// Parse malformed JSON
malformed := `{name: 'John', age: 30,}`
result, err := loosejson.ParseToJSON(malformed)
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
// Output:
// {
// "age": 30,
// "name": "John"
// }
}
Usage Examples
Example 1: Trailing Commas
input := `{"name": "John", "age": 30,}`
result, _ := loosejson.ParseToJSON(input)
fmt.Println(result)
// {"age": 30, "name": "John"}
Example 2: Unquoted Keys
input := `{name: "John", age: 30}`
result, _ := loosejson.ParseToJSON(input)
fmt.Println(result)
// {"age": 30, "name": "John"}
Example 3: Single Quotes
input := `{'name': 'Alice', 'city': 'New York'}`
result, _ := loosejson.ParseToJSON(input)
fmt.Println(result)
// {"city": "New York", "name": "Alice"}
input := `{
// User information
"name": "Bob",
/* Age field */
"age": 25
}`
result, _ := loosejson.ParseToJSON(input)
fmt.Println(result)
// {"age": 25, "name": "Bob"}
Example 5: Missing Closing Brackets
input := `{"items": [1, 2, 3`
result, _ := loosejson.ParseToJSON(input)
fmt.Println(result)
// {"items": [1, 2, 3]}
Example 6: Hex Numbers
input := `{"color": 0xFF00FF, "size": 0x100}`
result, _ := loosejson.ParseToJSON(input)
fmt.Println(result)
// {"color": 16711935, "size": 256}
Example 7: Unmarshal to Struct
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
input := []byte(`{name: 'David', age: 40,}`)
var person Person
err := loosejson.Unmarshal(input, &person)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", person)
// {Name:David Age:40}
input := `{
name: 'Charlie',
age: 35,
hobbies: ['reading', 'gaming',],
address: {
street: "123 Main St",
city: 'Boston',
zip: 0x2710, // 10000 in hex
},
active: true,
}`
result, _ := loosejson.ParseToJSON(input)
fmt.Println(result)
// Properly formatted JSON output
API Reference
Functions
Parses a malformed JSON string and returns the result as interface{}.
result, err := loosejson.ParseString(`{name: 'John'}`)
Parses malformed JSON bytes and returns the result as interface{}.
result, err := loosejson.ParseBytes([]byte(`{name: 'John'}`))
Parses malformed JSON and returns a properly formatted JSON string.
jsonStr, err := loosejson.ParseToJSON(`{name: 'John'}`)
Parses malformed JSON and unmarshals it into the given interface.
var data MyStruct
err := loosejson.Unmarshal([]byte(`{name: 'John'}`), &data)
| Issue |
Example |
Fixed Output |
| Trailing commas |
[1, 2, 3,] |
[1, 2, 3] |
| Unquoted keys |
{name: "John"} |
{"name": "John"} |
| Single quotes |
{'name': 'John'} |
{"name": "John"} |
| Comments |
{/* comment */ "a": 1} |
{"a": 1} |
| Missing brackets |
{"a": [1, 2 |
{"a": [1, 2]} |
| Hex numbers |
{"value": 0xFF} |
{"value": 255} |
| Multiple commas |
[1,,,2] |
[1, 2] |
| undefined |
{"a": undefined} |
{"a": null} |
Running Tests
go test -v
Running Examples
cd examples/basic
go run main.go
Comparison with Python's dirty-json
This library provides similar functionality to Python's dirty-json but for Go:
| Feature |
dirty-json (Python) |
loose-json-go |
| Trailing commas |
✅ |
✅ |
| Unquoted keys |
✅ |
✅ |
| Single quotes |
✅ |
✅ |
| Comments |
✅ |
✅ |
| Missing brackets |
✅ |
✅ |
| Hex numbers |
✅ |
✅ |
| Special values |
✅ |
✅ |
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature)
- Commit your changes (
git commit -m 'Add some AmazingFeature')
- Push to the branch (
git push origin feature/AmazingFeature)
- Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Inspired by Python's dirty-json library
- Built with Go's powerful parsing capabilities
Author
Song Guodong
Links