Logs
JSON Log Line (Single-Line Object)
Match a single-line JSON object — typical of structured logging from services like slog, Bunyan, or Pino.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^\\{(?:[^{}]|\\{[^{}]*\\})*\\}$", "");
const input = "{\"level\":\"info\",\"msg\":\"started\",\"port\":8080}";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
pyPython
import re
pattern = re.compile(r"^\{(?:[^{}]|\{[^{}]*\})*\}$")
input_text = "{\"level\":\"info\",\"msg\":\"started\",\"port\":8080}"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
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.
Pattern
regexengine-agnostic
^\{(?:[^{}]|\{[^{}]*\})*\}$Raw source: ^\{(?:[^{}]|\{[^{}]*\})*\}$
How it 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 lineNo match
—Common use cases
- •Splitting mixed-format log streams (text vs JSON)
- •Routing JSON lines to a parser, text to a different sink
- •Pre-filter for log shippers (Vector, Fluentd, Filebeat)
- •Log-line shape validation in observability pipelines
Related patterns
Java Stack Trace Line
LogsMatches a single Java stack trace frame line.
CloudWatch Log Stream Path
LogsMatch AWS CloudWatch log group / stream paths like `/aws/lambda/my-function` or `/aws/ecs/cluster-name`.
Apache Common Log Format
LogsParses Apache Common Log Format entries.
Log Level
LogsMatches standard log level keywords in log lines.