JSON Key-Value Pair (Simple)
Extract simple `"key": value` pairs from JSON-ish text (strings, numbers, booleans, null).
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("\"([^\"\\\\]+)\"\\s*:\\s*(\"[^\"\\\\]*\"|-?\\d+(?:\\.\\d+)?|true|false|null)", "g");
const input = "{\"name\": \"alice\", \"age\": 30, \"active\": true}";
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
import re
pattern = re.compile(r"\"([^\"\\\\]+)\"\\s*:\\s*(\"[^\"\\\\]*\"|-?\\d+(?:\\.\\d+)?|true|false|null)")
input_text = "{\"name\": \"alice\", \"age\": 30, \"active\": true}"
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
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.
Pattern
"([^"\\]+)"\s*:\s*("[^"\\]*"|-?\d+(?:\.\d+)?|true|false|null) (flags: g)Raw source: "([^"\\]+)"\s*:\s*("[^"\\]*"|-?\d+(?:\.\d+)?|true|false|null)
How it works
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
—Common use cases
- •Quick log-line scraping when JSON.parse would be overkill
- •Extracting fields from semi-JSON config formats (HCL, JSON5)
- •Search-and-grep over JSON lines (jq alternatives)
- •Static analysis of JSON snippets in source
Related patterns
JSON Boolean / Null Literal
Text ProcessingMatch JSON `true`, `false`, and `null` literal values, with word boundaries to avoid partial matches.
JSON Number (Strict)
Text ProcessingMatch JSON-spec numbers — disallows leading zeros (no `01`), allows decimals and exponents.
Logfmt Key-Value Pair
LogsParse key=value pairs from logfmt-style log lines, supporting both quoted and unquoted values.
Markdown Link
Text ProcessingMatch Markdown links [link text](url) and capture both the display text and the URL.