/mj

Make JSON from the command line…

Primary LanguageGo

MJ

Make JSON from the command line.

The program takes a list of key=value parameter on the command line:

$ mj foo=bar
{"foo":"bar"}

$ mj foo=bar baz=quux
{"baz":"quux","foo":"bar"}

The only requirement being that the keys be unique:

$ mj a=b a=c
Error: Key path was already assigned

A key can be a simple string, or a .-separated path of arbitrary depth:

$ mj foo.bar=baz
{"foo":{"bar":"baz"}}

There are command-line options to select a different separator for key-value pairs and for key paths:

$ mj foo:bar=baz
{"foo:bar":"baz"}

$ mj -s=: foo:bar=baz
{"foo":"bar=baz"}

$ mj -p=: foo:bar=baz
{"foo":{"bar":"baz"}}

$ mj -p='->' 'foo->bar=baz'
{"foo":{"bar":"baz"}}

If a key starts with -, it will be interpreted as a command line flag. To prevent that, use --:

$ mj -really=why
Error: flag provided but not defined: -really

$ mj -- -really=why
{"-really":"why"}

There is also support for slices on the last level:

$ mj foo[]=abc foo[]=def
{"foo":["abc","def"]}

But the operation is not supported on deeply-nested objects yet:

$ mj foo[].bar=abc foo[].bar=def
mj: encountered error while processing argument #0: "foo[].bar=abc"
	underlying error: while processing key path [foo[] bar]: cannot set key "bar" to "abc" on []interface {}: not supported

By default, values are interpreted as-is:

$ mj foo=@bar.txt
{"foo":"@bar.txt"}

However, you can designate certain prefixes to read values from a file:

$ echo hello-world > bar.txt
$ mj -r=@ foo=@bar.txt
{"foo":"hello-world\n"}