fixjson

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 2 Imported by: 2

README

fixjson

Go Reference

fixjson is a Go package that converts broken, nonstandard or nonofficial format (e.g. jsonc) to standard json. As an author of this package, I kindly ask you, stop doing that shit and use standard json whenever possible.

The features of the package are:

  • remove comments, single line (// text) or multiline (/* text */)
  • remove trailing commas, from arrays and objects
  • add missing commas after string literals at the end of a line in arrays and objects
  • remove unnecessary and invalid escape character of single quotes (')
  • escape quotes " in string literals, if they are placed not at the end of a line
  • fixes html encoded quotes in json
  • replace new line (\n) characters in string literals with \\ following by n
  • replace tabs (\t) characters with \\ following by t
{
  /* Example block                                  // multiline comment
   comment */
  "propertyName": {
    "name": "No, you can\'t do that in json",       // invalid escape character
    "value": 5432, // counter                       // inline comment           
    "username": "josh"                              // missing comma
    "password": "pass123",                          // trailing comma
  },

  "title": "Hello                                   // new line in string literal
    World",
}

Install

go get github.com/astappiev/fixjson@latest

Usage

package main

import (
	"encoding/json"
	"fmt"

	"github.com/astappiev/fixjson"
)

func main() {
	raw := []byte(`{
      /* Example block
       comment */
      "propertyName": {
        "name": "No, you can\'t do that in json",
        "value": 5432, // counter   
        "username": "josh"
        "password": "pass123",
      },
    
      "title": "Hello
        World",
    }`)

	var out map[string]any
	if err := fixjson.Unmarshal(raw, &out); err != nil {
		panic(err)
	}

	fmt.Println(out["title"])

	// Or fix first, then unmarshal manually.
	fixed := fixjson.ToJSON(raw)
	_ = json.Unmarshal(fixed, &out)
}

API

  • ToJSON(src []byte) []byte: translates input into valid JSON bytes
  • Unmarshal(data []byte, v any) error: fixes then unmarshals
  • FallbackUnmarshal(data []byte, v any) error: tries standard unmarshal first, then fix+unmarshal

Documentation

Overview

Package fixjson translates broken or non-standard JSON in src into valid JSON. The input is not modified; a new byte slice is returned.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FallbackUnmarshal

func FallbackUnmarshal(data []byte, v any) error

FallbackUnmarshal tries to unmarshal the data as valid JSON first. If that fails, it falls back to fixing it and unmarshaling the fixed JSON.

func ToJSON

func ToJSON(src []byte) []byte

ToJSON translates broken or non-standard JSON in src into valid JSON. The input is not modified; a new byte slice is returned.

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal tries to fix the JSON first, and if that succeeds but unmarshaling the fixed form fails, it falls back to the original raw input.

Types

This section is empty.

Jump to

Keyboard shortcuts

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