-
Notifications
You must be signed in to change notification settings - Fork 4
jsontool: Command line tool to process JSON files
codehero edited this page Sep 14, 2010
·
3 revisions
Command line processing of JSON files is one of my immediate goals. I would like to transform one or more JSON input files into either a new JSON file or some other text format. The tool will write all output to stdout
The tool will take as input:
- JSON Input: stdin, filenames, or directory name containing json files.
- Input script: describes how to transform the JSON input; specified as a file or contained in parameter
- Output JSON: Output new JSON file, enforce syntax compliance?
Sample invocations
- jsontool -i script.json < input.json > output.json
- jsontool -s “…” < input.json > output.json
- jsontool -i scr.json dir/*.json
The script language is inspired by JSON/T + some minor modifications.
Like JSON/T, the language will be one giant map, whose key strings are matching rules and whose associated values specify the output.
Differences from JSON/T:
- Instead of “self” identifying the root of the current JSON file, the empty key "" will name that rule instead.
- Instead of ‘{’ and ‘}’ characters escaping into rule specification, rule escapes will be bounded on both ends ‘\b’ (a pretty useless character). ‘\b’ is uglier to look at, but the code will be cleaner with a non-content escape character.
- Multiple JSON files may be passed in as input, so a specification for the fileset root is needed. This is indicated by the “\b” key.
- Consequently, it may be necessary to output the filenames. This is indicated by “\f” in a rule. “\f” in a key indicates a filename match. The matching string follows the ‘\f’ char. Also, \f[N], indicates matching the Nth file.
- ‘*’ prepended before a rule identifier indicates to dump all children of whatever matches that rule.
Examples:
Concatenate all json files in a directory into a map. Each key value pair is the (filename, json file content). All pairs will be prepended by a comma except for the first file.
script.json
{ "\b":"{ \b$\b }", "" : ",\"\f\" : \b*$\b", "\f[0]" : "\"\f\" : \b*$\b" }
@jsontool -i script.json dir/*.json > output.json@
Match against a particular file by name
script.json
{ "\b":"[\b\fX1\b , \b\fX2\b]", "\fX1":"\b$.value\b", "\fX2":"\b$.value\b" }
@jsontool -i script.json -n X1 i1.json -n X2 i2.json@