Skip to content

Implement JSON pretty print - #324

Merged
tksm merged 6 commits into
stern:masterfrom
fabn:feature/ppJson
Nov 22, 2024
Merged

tksm merged 6 commits into
stern:masterfrom
fabn:feature/ppJson

Conversation

@fabn

@fabn fabn commented Nov 19, 2024

Copy link
Copy Markdown
Contributor

Following what has been suggested in #323 I tried to implement a prettyJSON template function and a pretty flag for existing json.

My go skills are near to zero, so forgive me if I wrote bad code. I'm open to suggestions 😉

Basic usage:

# Output formatted json from object
{{- with $msg := .Message | tryParseJSON -}}
{{- json $msg true }}{{ "\n" }}
{{- end -}}
# Output formatted JSON from string
{{ .Message | prettyJSON }}

@tksm

tksm commented Nov 21, 2024

Copy link
Copy Markdown
Contributor

Hi, thanks for the PR!

How about enhancing prettyJSON to handle both JSON strings and JSON objects(map[string]any) directly, instead of introducing the pretty flag for json? This change might simplify the usage and make it more intuitive.

Updated prettyJSON

Since Go templates cannot handle errors, it returns the original string if an error occurs.

		"prettyJSON": func(value any) string {
			var data map[string]any

			switch v := value.(type) {
			case string:
				if err := json.Unmarshal([]byte(v), &data); err != nil {
					return v
				}
			case map[string]any:
				data = v
			default:
				return fmt.Sprintf("%v", value)
			}

			b, err := json.MarshalIndent(data, "", "  ")
			if err != nil {
				return fmt.Sprintf("%v", value)
			}

			return string(b)
		},

Example: Handling JSON strings

dist/stern --stdin --template \
  '{{ .Message | prettyJSON }}{{"\n"}}' <<EOS
{"level":"info","ts":1257894000,"caller":"sandbox2210433069/prog.go:10","msg":"hello","key1":"val1"}
{"level":"info","ts":1257894000,"caller":"sandbox2210433069/prog.go:13","msg":"world","key1":"val1"}
invalid json
EOS

Invalid JSON strings (such as invalid json) are printed as is.

{
  "caller": "sandbox2210433069/prog.go:10",
  "key1": "val1",
  "level": "info",
  "msg": "hello",
  "ts": 1257894000
}
{
  "caller": "sandbox2210433069/prog.go:13",
  "key1": "val1",
  "level": "info",
  "msg": "world",
  "ts": 1257894000
}
invalid json

Example: Handling JSON objects

dist/stern --stdin --template \
  '{{ with $msg := .Message | tryParseJSON }}{{ prettyJSON $msg }}{{"\n"}}{{end}}' <<EOS
{"level":"info","ts":1257894000,"caller":"sandbox2210433069/prog.go:10","msg":"hello","key1":"val1"}
{"level":"info","ts":1257894000,"caller":"sandbox2210433069/prog.go:13","msg":"world","key1":"val1"}
invalid json
EOS

Invalid JSON strings (such as invalid json) are ignored because of the with clause.

{
  "caller": "sandbox2210433069/prog.go:10",
  "key1": "val1",
  "level": "info",
  "msg": "hello",
  "ts": 1257894000
}
{
  "caller": "sandbox2210433069/prog.go:13",
  "key1": "val1",
  "level": "info",
  "msg": "world",
  "ts": 1257894000
}

@fabn

fabn commented Nov 21, 2024

Copy link
Copy Markdown
Contributor Author

Awesome, will do that.

@fabn

fabn commented Nov 22, 2024

Copy link
Copy Markdown
Contributor Author

@tksm I pushed your suggestion, let me know

@fabn

fabn commented Nov 22, 2024

Copy link
Copy Markdown
Contributor Author

I guess I should update the doc as well, but I want to wait for your review before doing that

@tksm

tksm commented Nov 22, 2024

Copy link
Copy Markdown
Contributor

LGTM for the prettyJSON func 👍

@tksm

tksm commented Nov 22, 2024

Copy link
Copy Markdown
Contributor

📝 I have already removed extjson and ppextjson from the template function list in #325, but thank you for updating it anyway.

@fabn

fabn commented Nov 22, 2024

Copy link
Copy Markdown
Contributor Author

LGTM for the prettyJSON func 👍

I added some examples in the readme and removed outdated options.

Comment thread README.md Resolved
Co-authored-by: Takashi Kusumi <[email protected]>

@tksm tksm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tksm
tksm merged commit ccd8add into stern:master Nov 22, 2024
@fabn
fabn deleted the feature/ppJson branch November 22, 2024 11:05
@dylanrb123

Copy link
Copy Markdown

Hello, I just found this and would love to try to use it but it looks like it hasn't been released. Any chance you could cut a new version? Thanks!

@superbrothers

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants