Colorized JSON output for Go

jsoncolor

GoDoc

jsoncolor is a drop-in replacement for encoding/json's Marshal and MarshalIndent functions and Encoder type which produce colorized output using fatih's color package.

Installation

go get -u github.com/nwidger/jsoncolor

Usage

To use as a replacement for encoding/json, exchange

import "encoding/json" with import json "github.com/nwidger/jsoncolor".

json.Marshal, json.MarshalIndent and json.NewEncoder will now produce colorized output.

Custom Colors

The colors used for each type of token can be customized by creating a custom Formatter, changing its XXXColor fields and then passing it to MarshalWithFormatter, MarshalIndentWithFormatter or NewEncoderWithFormatter. If a XXXColor field of the custom Formatter is not set, the corresponding DefaultXXXColor package variable is used. See color.New for creating custom color values and the GoDocs for the default colors.

import (
        "fmt"
        "log"

        "github.com/fatih/color"
        json "github.com/nwidger/jsoncolor"
)

// create custom formatter
f := json.NewFormatter()

// set custom colors
f.StringColor = color.New(color.FgBlack, color.Bold)
f.TrueColor = color.New(color.FgWhite, color.Bold)
f.FalseColor = color.New(color.FgRed)
f.NumberColor = color.New(color.FgWhite)
f.NullColor = color.New(color.FgWhite, color.Bold)

// marshal v with custom formatter,
// dst contains colorized output
dst, err := json.MarshalWithFormatter(v, f)
if err != nil {
        log.Fatal(err)
}

// print colorized output to stdout
fmt.Println(string(dst))
Similar Resources

JSON query in Golang

gojq JSON query in Golang. Install go get -u github.com/elgs/gojq This library serves three purposes: makes parsing JSON configuration file much easie

Dec 28, 2022

Automatically generate Go (golang) struct definitions from example JSON

gojson gojson generates go struct definitions from json or yaml documents. Example $ curl -s https://api.github.com/repos/chimeracoder/gojson | gojson

Jan 1, 2023

A JSON diff utility

JayDiff A JSON diff utility. Install Downloading the compiled binary Download the latest version of the binary: releases extract the archive and place

Dec 11, 2022

Fast and flexible JSON encoder for Go

Fast and flexible JSON encoder for Go

Jettison Jettison is a fast and flexible JSON encoder for the Go programming language, inspired by bet365/jingo, with a richer features set, aiming at

Dec 21, 2022

Create go type representation from json

json2go Package json2go provides utilities for creating go type representation from json inputs. Json2go can be used in various ways: CLI tool Web pag

Dec 26, 2022

Console JSON formatter with query feature

Console JSON formatter with query feature

Console JSON formatter with query feature. Install: $ go get github.com/miolini/jsonf Usage: Usage of jsonf: -c=true: colorize output -d=false: de

Dec 4, 2022

Fluent API to make it easier to create Json objects.

Jsongo Fluent API to make it easier to create Json objects. Install go get github.com/ricardolonga/jsongo Usage To create this: { "name":"Ricar

Nov 7, 2022

Arbitrary transformations of JSON in Golang

kazaam Description Kazaam was created with the goal of supporting easy and fast transformations of JSON data with Golang. This functionality provides

Dec 18, 2022

Parsing JSON is a hassle in golang

GoJSON Parsing JSON is a hassle in golang. This package will allow you to parse and search elements in a json without structs. Install gojson go get g

Nov 12, 2021
Comments
  • Encoder does not print newlines

    Encoder does not print newlines

    Here's an example: https://goplay.space/#aJgJu9bSfOV

    From looking at the code, it looks like the encoder prints a newline via printSpace which is disabled if compact=true. AFAICT, those are only relevant to MarshalIndent.

  • Create a release tag

    Create a release tag

    I am currently working on packaging jsoncolor as a dependency of another project which I am porting to OpenBSD. OpenBSD packaging requires that a version be associated with the software. Would it be possible to tag a release of jsoncolor?

  • `&` character displayed as `\u0026` in strings

    `&` character displayed as `\u0026` in strings

    I couldn't find any option to disable this behavior so I suppose, it is a bug.

    Example JSON to reproduce:

    {
      "url": "https://httpbin.org/get?x=3&y=3"
    }
    

    Could you confirm?

  • Getting a undefined error when installing jsoncolor

    Getting a undefined error when installing jsoncolor

    go get -u github.com/nwidger/jsoncolor

    github.com/nwidger/jsoncolor

    .go/src/github.com/nwidger/jsoncolor/jsoncolor.go:225: enc.SetEscapeHTML undefined (type *json.Encoder has no field or method SetEscapeHTML)

    Version:

    go version go version go1.6.3 linux/amd64

Related tags
Get JSON values quickly - JSON parser for Go
Get JSON values quickly - JSON parser for Go

get json values quickly GJSON is a Go package that provides a fast and simple way to get values from a json document. It has features such as one line

Dec 28, 2022
JSON diff library for Go based on RFC6902 (JSON Patch)

jsondiff jsondiff is a Go package for computing the diff between two JSON documents as a series of RFC6902 (JSON Patch) operations, which is particula

Dec 4, 2022
Fast JSON encoder/decoder compatible with encoding/json for Go
Fast JSON encoder/decoder compatible with encoding/json for Go

Fast JSON encoder/decoder compatible with encoding/json for Go

Jan 6, 2023
Package json implements encoding and decoding of JSON as defined in RFC 7159

Package json implements encoding and decoding of JSON as defined in RFC 7159. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions

Jun 26, 2022
Json-go - CLI to convert JSON to go and vice versa
Json-go - CLI to convert JSON to go and vice versa

Json To Go Struct CLI Install Go version 1.17 go install github.com/samit22/js

Jul 29, 2022
JSON Spanner - A Go package that provides a fast and simple way to filter or transform a json document

JSON SPANNER JSON Spanner is a Go package that provides a fast and simple way to

Sep 14, 2022
Abstract JSON for golang with JSONPath support

Abstract JSON Abstract JSON is a small golang package provides a parser for JSON with support of JSONPath, in case when you are not sure in its struct

Jan 5, 2023
Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection

fastjson - fast JSON parser and validator for Go Features Fast. As usual, up to 15x faster than the standard encoding/json. See benchmarks. Parses arb

Jan 5, 2023
Small utility to create JSON objects
Small utility to create JSON objects

gjo Small utility to create JSON objects. This was inspired by jpmens/jo. Support OS Mac Linux Windows Requirements Go 1.1.14~ Git Installtion Build $

Dec 8, 2022
A Go package for handling common HTTP JSON responses.

go-respond A Go package for handling common HTTP JSON responses. Installation go get github.com/nicklaw5/go-respond Usage The goal of go-respond is to

Sep 26, 2022