JSON Key-Value Pair (Simple) in GO
Extract simple `"key": value` pairs from JSON-ish text (strings, numbers, booleans, null).
Try it in the GO tester →Pattern
regexGO
"([^"\\]+)"\s*:\s*("[^"\\]*"|-?\d+(?:\.\d+)?|true|false|null) (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`"([^"\\]+)"\s*:\s*("[^"\\]*"|-?\d+(?:\.\d+)?|true|false|null)`)
input := `{"name": "alice", "age": 30, "active": true}`
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
"([^"\\]+)" captures the key — non-quote, non-backslash chars (so escaped quotes break the match — by design, this is a quick parser, not a strict one). \s*:\s* matches the separator. The value group covers strings, signed integers/decimals, true, false, and null. Use a real JSON parser for production!
Examples
Input
{"name": "alice", "age": 30, "active": true}Matches
"name": "alice""age": 30"active": true
Input
"timeout": 5000, "retries": nullMatches
"timeout": 5000"retries": null
Input
no json hereNo match
—