{"id":13413266,"url":"https://github.com/neilotoole/jsoncolor","last_synced_at":"2025-09-11T21:17:55.802Z","repository":{"id":43396555,"uuid":"405798613","full_name":"neilotoole/jsoncolor","owner":"neilotoole","description":"Colorized JSON output for Go","archived":false,"fork":false,"pushed_at":"2024-02-12T13:48:41.000Z","size":13994,"stargazers_count":47,"open_issues_count":7,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-11T09:08:22.461Z","etag":null,"topics":["color","colors","go","golang","json"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/neilotoole.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-09-13T01:44:14.000Z","updated_at":"2025-04-08T18:57:22.000Z","dependencies_parsed_at":"2024-06-18T22:32:52.766Z","dependency_job_id":"790f882f-46d0-46b8-8b4d-10e331f740ab","html_url":"https://github.com/neilotoole/jsoncolor","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/neilotoole/jsoncolor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neilotoole%2Fjsoncolor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neilotoole%2Fjsoncolor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neilotoole%2Fjsoncolor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neilotoole%2Fjsoncolor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/neilotoole","download_url":"https://codeload.github.com/neilotoole/jsoncolor/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neilotoole%2Fjsoncolor/sbom","scorecard":{"id":678861,"data":{"date":"2025-08-11","repo":{"name":"github.com/neilotoole/jsoncolor","commit":"1637fae69be1e115df81c7015d6847675b4cf8d4"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":4.8,"checks":[{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/go.yml:1","Info: topLevel 'contents' permission set to 'read': .github/workflows/golangci-lint.yml:11","Info: topLevel 'pull-requests' permission set to 'read': .github/workflows/golangci-lint.yml:13","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Code-Review","score":1,"reason":"Found 4/25 approved changesets -- score normalized to 1","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Security-Policy","score":10,"reason":"security policy file detected","details":["Info: security policy file detected: SECURITY.md:1","Info: Found linked content: SECURITY.md:1","Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1","Info: Found text in security policy: SECURITY.md:1"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Pinned-Dependencies","score":3,"reason":"dependency not pinned by hash detected -- score normalized to 3","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/go.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/neilotoole/jsoncolor/go.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/go.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/neilotoole/jsoncolor/go.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/golangci-lint.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/neilotoole/jsoncolor/golangci-lint.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/golangci-lint.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/neilotoole/jsoncolor/golangci-lint.yml/master?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/golangci-lint.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/neilotoole/jsoncolor/golangci-lint.yml/master?enable=pin","Info:   0 out of   4 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   1 third-party GitHubAction dependencies pinned","Info:   1 out of   1 goCommand dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":-1,"reason":"internal error: error during branchesHandler.setup: internal error: githubv4.Query: Resource not accessible by integration","details":null,"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 13 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-21T22:27:04.463Z","repository_id":43396555,"created_at":"2025-08-21T22:27:04.463Z","updated_at":"2025-08-21T22:27:04.463Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274706618,"owners_count":25334769,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-11T02:00:13.660Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["color","colors","go","golang","json"],"created_at":"2024-07-30T20:01:36.450Z","updated_at":"2025-09-11T21:17:55.768Z","avatar_url":"https://github.com/neilotoole.png","language":"Go","funding_links":[],"categories":["JSON","Relational Databases"],"sub_categories":["Search and Analytic Databases","检索及分析资料库","Advanced Console UIs"],"readme":"[![Actions Status](https://github.com/neilotoole/jsoncolor/workflows/Go/badge.svg)](https://github.com/neilotoole/jsoncolor/actions?query=workflow%3AGo)\n[![Go Report Card](https://goreportcard.com/badge/neilotoole/jsoncolor)](https://goreportcard.com/report/neilotoole/jsoncolor)\n[![release](https://img.shields.io/badge/release-v0.7.0-green.svg)](https://github.com/neilotoole/jsoncolor/releases/tag/v0.7.0)\n[![Go Reference](https://pkg.go.dev/badge/github.com/neilotoole/jsoncolor.svg)](https://pkg.go.dev/github.com/neilotoole/jsoncolor)\n[![license](https://img.shields.io/github/license/neilotoole/jsoncolor)](./LICENSE)\n\n# jsoncolor\n\nPackage `neilotoole/jsoncolor` is a drop-in replacement for stdlib\n[`encoding/json`](https://pkg.go.dev/encoding/json) that outputs colorized JSON.\n\nWhy? Well, [`jq`](https://jqlang.github.io/jq/) colorizes its output by default, and color output\nis desirable for many Go CLIs. This package performs colorization (and indentation) inline\nin the encoder, and is significantly faster than stdlib at indentation.\n\nFrom the example [`jc`](./cmd/jc/main.go) app:\n\n![jsoncolor-output](./splash.png)\n\n## Usage\n\nGet the package per the normal mechanism (requires Go 1.16+):\n\n```shell\ngo get -u github.com/neilotoole/jsoncolor\n```\n\nThen:\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/mattn/go-colorable\"\n  json \"github.com/neilotoole/jsoncolor\"\n  \"os\"\n)\n\nfunc main() {\n  var enc *json.Encoder\n\n  // Note: this check will fail if running inside Goland (and\n  // other IDEs?) as IsColorTerminal will return false.\n  if json.IsColorTerminal(os.Stdout) {\n    // Safe to use color\n    out := colorable.NewColorable(os.Stdout) // needed for Windows\n    enc = json.NewEncoder(out)\n\n    // DefaultColors are similar to jq\n    clrs := json.DefaultColors()\n\n    // Change some values, just for fun\n    clrs.Bool = json.Color(\"\\x1b[36m\") // Change the bool color\n    clrs.String = json.Color{}         // Disable the string color\n\n    enc.SetColors(clrs)\n  } else {\n    // Can't use color; but the encoder will still work\n    enc = json.NewEncoder(os.Stdout)\n  }\n\n  m := map[string]interface{}{\n    \"a\": 1,\n    \"b\": true,\n    \"c\": \"hello\",\n  }\n\n  if err := enc.Encode(m); err != nil {\n    fmt.Fprintln(os.Stderr, err)\n    os.Exit(1)\n  }\n}\n```\n\n### Configuration\n\nTo enable colorization, invoke [`enc.SetColors`](https://pkg.go.dev/github.com/neilotoole/jsoncolor#Encoder.SetColors).\n\nThe [`Colors`](https://pkg.go.dev/github.com/neilotoole/jsoncolor#Colors) struct\nholds color config. The zero value and `nil` are both safe for use (resulting in no colorization).\n\nThe [`DefaultColors`](https://pkg.go.dev/github.com/neilotoole/jsoncolor#DefaultColors) func\nreturns a `Colors` struct that produces results similar to `jq`:\n\n```go\n// DefaultColors returns the default Colors configuration.\n// These colors largely follow jq's default colorization,\n// with some deviation.\nfunc DefaultColors() *Colors {\n  return \u0026Colors{\n    Null:   Color(\"\\x1b[2m\"),\n    Bool:   Color(\"\\x1b[1m\"),\n    Number: Color(\"\\x1b[36m\"),\n    String: Color(\"\\x1b[32m\"),\n    Key:    Color(\"\\x1b[34;1m\"),\n    Bytes:  Color(\"\\x1b[2m\"),\n    Time:   Color(\"\\x1b[32;2m\"),\n    Punc:   Color{}, // No colorization\n  }\n}\n```\n\nAs seen above, use the `Color` zero value (`Color{}`) to\ndisable colorization for that JSON element.\n\n### Helper for `fatih/color`\n\nIt can be inconvenient to use terminal codes, e.g. `json.Color(\"\\x1b[36m\")`.\nA helper package provides an adapter for [`fatih/color`](https://github.com/fatih/color).\n\n```go\n  // import \"github.com/neilotoole/jsoncolor/helper/fatihcolor\"\n  // import \"github.com/fatih/color\"\n  // import \"github.com/mattn/go-colorable\"\n  \n  out := colorable.NewColorable(os.Stdout) // needed for Windows\n  enc = json.NewEncoder(out)\n  \n  fclrs := fatihcolor.DefaultColors()\n  // Change some values, just for fun\n  fclrs.Number = color.New(color.FgBlue)\n  fclrs.String = color.New(color.FgCyan)\n  \n  clrs := fatihcolor.ToCoreColors(fclrs)\n  enc.SetColors(clrs)\n```\n\n### Drop-in for `encoding/json`\n\nThis package is a full drop-in for stdlib [`encoding/json`](https://pkg.go.dev/encoding/json)\n(thanks to the ancestral [`segmentio/encoding/json`](https://pkg.go.dev/github.com/segmentio/encoding/json)\npkg being a full drop-in).\n\nTo drop-in, just use an import alias:\n\n```go\n  import json \"github.com/neilotoole/jsoncolor\"\n```\n\n## Example app: `jc`\n\nSee [`cmd/jc`](cmd/jc/main.go) for a trivial CLI implementation that can accept JSON input,\nand output that JSON in color.\n\n```shell\n# From project root\n$ go install ./cmd/jc\n$ cat ./testdata/sakila_actor.json | jc\n```\n\n## Benchmarks\n\nNote that this package contains [`golang_bench_test.go`](./golang_bench_test.go), which\nis inherited from `segmentj`. But here we're interested in [`benchmark_test.go:BenchmarkEncode`](./benchmark_test.go),\nwhich benchmarks encoding performance versus other JSON encoder packages.\nThe results below benchmark the following:\n\n- Stdlib [`encoding/json`](https://pkg.go.dev/encoding/json) (`go1.17.1`).\n- [`segmentj`](https://github.com/segmentio/encoding): `v0.1.14`, which was when `jsoncolor` was forked. The newer `segmentj` code performs even better.\n- `neilotoole/jsoncolor`: (this package) `v0.6.0`.\n- [`nwidger/jsoncolor`](https://github.com/nwidger/jsoncolor): `v0.3.0`, latest at time of benchmarks.\n\nNote that two other Go JSON colorization packages ([`hokaccha/go-prettyjson`](https://github.com/hokaccha/go-prettyjson) and\n[`TylerBrock/colorjson`](https://github.com/TylerBrock/colorjson)) are excluded from\nthese benchmarks because they do not provide a stdlib-compatible `Encoder` impl.\n\n```\n$ go test -bench=BenchmarkEncode -benchtime=\"5s\"\ngoarch: amd64\npkg: github.com/neilotoole/jsoncolor\ncpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz\nBenchmarkEncode/stdlib_NoIndent-16                           181          33047390 ns/op         8870685 B/op     120022 allocs/op\nBenchmarkEncode/stdlib_Indent-16                             124          48093178 ns/op        10470366 B/op     120033 allocs/op\nBenchmarkEncode/segmentj_NoIndent-16                         415          14658699 ns/op         3788911 B/op      10020 allocs/op\nBenchmarkEncode/segmentj_Indent-16                           195          30628798 ns/op         5404492 B/op      10025 allocs/op\nBenchmarkEncode/neilotoole_NoIndent_NoColor-16               362          16522399 ns/op         3789034 B/op      10020 allocs/op\nBenchmarkEncode/neilotoole_Indent_NoColor-16                 303          20146856 ns/op         5460753 B/op      10021 allocs/op\nBenchmarkEncode/neilotoole_NoIndent_Color-16                 295          19989420 ns/op        10326019 B/op      10029 allocs/op\nBenchmarkEncode/neilotoole_Indent_Color-16                   246          24714163 ns/op        11996890 B/op      10030 allocs/op\nBenchmarkEncode/nwidger_NoIndent_NoColor-16                   10         541107983 ns/op        92934231 B/op    4490210 allocs/op\nBenchmarkEncode/nwidger_Indent_NoColor-16                      7         798088086 ns/op        117258321 B/op   6290213 allocs/op\nBenchmarkEncode/nwidger_indent_NoIndent_Colo-16               10         542002051 ns/op        92935639 B/op    4490224 allocs/op\nBenchmarkEncode/nwidger_indent_Indent_Color-16                 7         799928353 ns/op        117259195 B/op   6290220 allocs/op\n```\n\nAs always, take benchmarks with a large grain of salt, as they're based on a (small) synthetic benchmark.\nMore benchmarks would give a better picture (and note as well that the benchmarked `segmentj` is an older version, `v0.1.14`).\n\nAll that having been said, what can we surmise from these particular results?\n\n- `segmentj` performs better than `stdlib` at all encoding tasks.\n- `jsoncolor` performs better than `segmentj` for indentation (which makes sense, as indentation is performed inline).\n- `jsoncolor` performs better than `stdlib` at all encoding tasks.\n\nAgain, trust these benchmarks at your peril. Create your own benchmarks for your own workload.\n\n## Notes\n\n- The [`.golangci.yml`](./.golangci.yml) linter settings have been fiddled with to hush some\n  linting issues inherited from the `segmentio` codebase at the time of forking. Thus, the linter report\n  may not be of great use. In an ideal world, the `jsoncolor` functionality would be [ported](https://github.com/neilotoole/jsoncolor/issues/15) to a\n  more recent (and better-linted) version of the `segementio` codebase.\n- The `segmentio` encoder (at least as of `v0.1.14`) encodes `time.Duration` as string, while `stdlib` outputs as `int64`.\n  This package follows `stdlib`.\n- The [`Colors.Punc`](https://pkg.go.dev/github.com/neilotoole/jsoncolor#Colors) field controls all\n  punctuation colorization, i.e. `[]{},:\"`. It is probably worthwhile to [separate](https://github.com/neilotoole/jsoncolor/issues/16)\n  these out into individually-configurable elements.\n\n\u003ca name=\"history\"\u003e\u003c/a\u003e\n## CHANGELOG\n\nHistory: this package is an extract of [`sq`](https://github.com/neilotoole/sq)'s JSON encoding package, which itself is a fork of the\n[`segmentio/encoding`](https://github.com/segmentio/encoding) JSON encoding package. Note that the\noriginal `sq` JSON encoder was forked from Segment's codebase at `v0.1.14`, so\nthe codebases have drifted significantly by now.\n\n### [v0.7.1](https://github.com/neilotoole/jsoncolor/releases/tag/v0.7.1)\n\n- [#27](https://github.com/neilotoole/jsoncolor/pull/27): Improved Windows terminal color support checking.\n\n### [v0.7.0](https://github.com/neilotoole/jsoncolor/releases/tag/v0.7.0)\n\n- [#21](https://github.com/neilotoole/jsoncolor/pull/21): Support for [`encoding.TextMarshaler`](https://pkg.go.dev/encoding#TextMarshaler).\n- [#22](https://github.com/neilotoole/jsoncolor/pull/22): Removed redundant dependencies.\n- [#26](https://github.com/neilotoole/jsoncolor/pull/26): Updated dependencies.\n\n## Acknowledgments\n\n- [`jq`](https://stedolan.github.io/jq/): sine qua non.\n- [`segmentio/encoding`](https://github.com/segmentio/encoding): `jsoncolor` is layered into Segment's JSON encoder. They did the hard work. Much gratitude to that team.\n- [`sq`](https://github.com/neilotoole/sq): `jsoncolor` is effectively an extract of code created specifically for `sq`.\n- [`mattn/go-colorable`](https://github.com/mattn/go-colorable): no project is complete without `mattn` having played a role.\n- [`fatih/color`](https://github.com/fatih/color): the color library.\n- [`@hermannm`](https://github.com/hermannm): for several PRs.\n\n### Related\n\n- [`nwidger/jsoncolor`](https://github.com/nwidger/jsoncolor)\n- [`hokaccha/go-prettyjson`](https://github.com/hokaccha/go-prettyjson)\n- [`TylerBrock/colorjson`](https://github.com/TylerBrock/colorjson)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneilotoole%2Fjsoncolor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fneilotoole%2Fjsoncolor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneilotoole%2Fjsoncolor/lists"}