Python (re)

JSON Log Line (Single-Line Object) in PY

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

Try it in the PY tester →

Pattern

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

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+.

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)