2 ms·
Thought experiment: What would AWK for JSON/XML look like?
AWK is a really powerful tool for working on (mostly) line-based data. It feels like the semantics of "if PATTERN matches RECORD, run this ACTION" could be very powerful when applied to many data streams:
- JSON
- CSV
- Apache log data after GROKing into a KV pairs
- HTML or XML
And the list goes on...
AWK supports "PATTERNS" like:
- /REGEX/ (Matching this regex)
- /REGEX/,/REGEX/ (matches all lines from first regex match to second, think "/CREATE TABLE/,/);/")
- "$1 == 200" (If the first column is 200)
- "$4 ~ /Technology/" (The 4th column matches the regex)
So for structured data it could be something like:
- "record['status'] == 200 { successful_bytes += record['bytes_tx'] }"
But for JSON, because of nesting, it probably needs some way of indicating what enclosing context to operate on, like maybe:
- "[/status/: =200]" (the list element that has a status key with a value of 200)
- "[[/status/: =404]]" (A list element with a child that has status:404)
- "[/error/:[/message/: /connection closed/]]" (list element element that has "error", which has an element below it named "message" that contains "connection closed")
The inspiration for this question is "jq", which has a bunch of filters and selectors and pipes, and the "elvish" shell which has it's own operators for working on JSON data as part of shell pipelines.
Any thoughts about what AWK for JSON (and others) would look like?