Summary
A new, non-standard builtin providing simple JSON operations for the 90% case. jq is powerful but has a learning curve. json would cover common get/set/merge operations with intuitive syntax.
Note: This is a bashkit-specific extension, not a standard command. jq remains available for complex queries.
Proposed Syntax
json <subcommand> [args...] [< input]
Proposed Subcommands
| Subcommand |
Description |
| `json get .path` |
Extract value at JSON path |
| `json set .path value` |
Set value at JSON path |
| `json delete .path` |
Remove key at JSON path |
| `json merge file1 file2` |
Deep merge two JSON objects |
| `json keys` |
List top-level keys |
| `json type .path` |
Print type (string, number, object, array, bool, null) |
| `json valid` |
Validate JSON (exit 0 if valid, 1 if not) |
| `json fmt` |
Pretty-print JSON |
| `json compact` |
Minify JSON |
| `json each .array` |
Iterate array, output one element per line |
Use Cases
# Get a value
echo '{"name":"bashkit","version":"0.1"}' | json get .version
# 0.1
# Set a value
echo '{"name":"bashkit"}' | json set .version "0.2"
# {"name":"bashkit","version":"0.2"}
# Merge configs
json merge defaults.json overrides.json > config.json
# Validate
if echo "$response" | json valid; then
echo "$response" | json get .data.id
fi
# Iterate array
echo '{"items":[1,2,3]}' | json each .items
# 1
# 2
# 3
# Pipeline with set
cat config.json | json set .database.host "localhost" | json set .database.port 5432 > updated.json
Implementation Notes
- Uses dot notation for paths:
.foo.bar[0].baz
- Array access:
.items[0], .items[-1] (last), .items[*] (all)
json get outputs raw value for strings (no quotes), quoted for objects/arrays
json set with numeric value → JSON number; quoted → JSON string
- Could reuse the existing
serde_json dependency
- NOT a replacement for
jq — complex filters/transforms should still use jq
- Exit codes: 0 = success, 1 = path not found or invalid JSON
Summary
A new, non-standard builtin providing simple JSON operations for the 90% case.
jqis powerful but has a learning curve.jsonwould cover common get/set/merge operations with intuitive syntax.Note: This is a bashkit-specific extension, not a standard command.
jqremains available for complex queries.Proposed Syntax
Proposed Subcommands
Use Cases
Implementation Notes
.foo.bar[0].baz.items[0],.items[-1](last),.items[*](all)json getoutputs raw value for strings (no quotes), quoted for objects/arraysjson setwith numeric value → JSON number; quoted → JSON stringserde_jsondependencyjq— complex filters/transforms should still usejq