How To: RegEx to validate JSON

This pattern will strictly validate a JSON string.

(?x-i)		# FREE-SPACE MODE, case-sensitive

(?(DEFINE)
	(?<ws>[\\r\
\\t\\x20]*)
	(?<str>"(?:\\\\[rntbf\\\\/] | \\\\u[[:xdigit:]]{4} | [^\\\"[:cntrl:]])*")
	(?<bool>true|false)
	(?<nil>nil)
	(?<num>-?\\d+(?:\\.\\d+)?)
	(?<elem>(?:(?&str)|(?&bool)|(?&nil)|(?&num))(?&ws))
	(?<comma>,(?&ws))
)

# JSON Array
\\[ (?&ws)
(?:
	(?:
		(?&elem) | (?R)(?&ws)
	)
	(?(?=(?&comma)(?:(?&elem)|[\\[\\{]))(?&comma))
)*
\\]

|	# Or

# JSON Object
\\{ (?&ws)
(?:
	(?&str) (?&ws) 
	: (?&ws)
	(?:
		(?&elem) | (?R)(?&ws)
	)
	(?(?=(?&comma)["\\[\\{])(?&comma))
)*

\\}

@Kem Tekinay Just what I needed! Thanks. :slight_smile: