Go (RE2)

JSON Log Line (Single-Line Object) in GO

Match a single-line JSON object — typical of structured logging from services like slog, Bunyan, or Pino.

Try it in the GO tester →

Pattern

regexGO
^\{(?:[^{}]|\{[^{}]*\})*\}$

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`^\{(?:[^{}]|\{[^{}]*\})*\}$`)
	input := `{"level":"info","msg":"started","port":8080}`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

How the pattern works

^\{ anchors to an opening brace at start. (?:[^{}]|\{[^{}]*\})* matches any non-brace chars or one level of nested braces (so `{"a":{"b":1}}` matches but deeper nesting may not). \}$ anchors to the closing brace at end. Quick filter for log-line shape; pair with JSON.parse to validate fully.

Examples

Input

{"level":"info","msg":"started","port":8080}

Matches

  • {"level":"info","msg":"started","port":8080}

Input

{"event":"login","user":{"id":42}}

Matches

  • {"event":"login","user":{"id":42}}

Input

plain text log line

No match

Same pattern, other engines

← Back to JSON Log Line (Single-Line Object) overview (all engines)