loosejson

package module
v0.0.0-...-47b0300 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 5 Imported by: 0

README

loose-json-go

Go Reference Go Report Card

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"}
Example 4: Comments
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}
Example 8: Complex Malformed JSON
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
ParseString(input string) (interface{}, error)

Parses a malformed JSON string and returns the result as interface{}.

result, err := loosejson.ParseString(`{name: 'John'}`)
ParseBytes(input []byte) (interface{}, error)

Parses malformed JSON bytes and returns the result as interface{}.

result, err := loosejson.ParseBytes([]byte(`{name: 'John'}`))
ParseToJSON(input string) (string, error)

Parses malformed JSON and returns a properly formatted JSON string.

jsonStr, err := loosejson.ParseToJSON(`{name: 'John'}`)
Unmarshal(input []byte, v interface{}) error

Parses malformed JSON and unmarshals it into the given interface.

var data MyStruct
err := loosejson.Unmarshal([]byte(`{name: 'John'}`), &data)

Supported Malformations

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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. 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

Documentation

Overview

Package loosejson provides a flexible JSON parser that can handle malformed or "loose" JSON data.

This library is inspired by Python's dirty-json and can parse JSON with various formatting issues:

  • Trailing commas in objects and arrays
  • Unquoted object keys
  • Single quotes instead of double quotes
  • Comments (single-line // and multi-line /* */)
  • Missing closing brackets and braces (auto-closes)
  • Hexadecimal numbers (e.g., 0xFF)
  • Special values (NaN, Infinity, undefined)
  • Multiple consecutive commas
  • Mixed quote styles
  • Scientific notation

Basic usage:

import loosejson "github.com/nightosong/loose-json-go"

// Parse malformed JSON to interface{}
result, err := loosejson.ParseString(`{name: 'John', age: 30,}`)

// Parse to formatted JSON string
jsonStr, err := loosejson.ParseToJSON(`{name: 'John', age: 30,}`)

// Unmarshal to struct
type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}
var person Person
err := loosejson.Unmarshal([]byte(`{name: 'John', age: 30,}`), &person)

The parser is designed to be lenient and forgiving, automatically fixing common JSON formatting errors while preserving the data structure and values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseBytes

func ParseBytes(input []byte) (interface{}, error)

ParseBytes parses malformed JSON bytes and returns the result as interface{}

func ParseString

func ParseString(input string) (interface{}, error)

ParseString parses a malformed JSON string and returns the result as interface{}

func ParseToJSON

func ParseToJSON(input string) (string, error)

ParseToJSON parses malformed JSON and returns a properly formatted JSON string

func Unmarshal

func Unmarshal(input []byte, v interface{}) error

Unmarshal parses malformed JSON and unmarshals it into the given interface

Types

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer represents a lexical scanner

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

NextToken returns the next token from the input

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser represents a JSON parser that can handle malformed JSON

func NewParser

func NewParser(input string) *Parser

NewParser creates a new parser

func (*Parser) Parse

func (p *Parser) Parse() (interface{}, error)

Parse parses the input and returns the result as interface{}

type Token

type Token struct {
	Type  TokenType
	Value string
	Pos   int
}

Token represents a lexical token

type TokenType

type TokenType int

TokenType represents the type of token

const (
	TokenError TokenType = iota
	TokenEOF
	TokenLeftBrace    // {
	TokenRightBrace   // }
	TokenLeftBracket  // [
	TokenRightBracket // ]
	TokenColon        // :
	TokenComma        // ,
	TokenString
	TokenNumber
	TokenTrue
	TokenFalse
	TokenNull
	TokenNaN
	TokenInfinity
	TokenNegInfinity
)

Directories

Path Synopsis
examples
advanced command
basic command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL