https://github.com/ripta/mj
Make JSON from the command line…
https://github.com/ripta/mj
Last synced: 5 months ago
JSON representation
Make JSON from the command line…
- Host: GitHub
- URL: https://github.com/ripta/mj
- Owner: ripta
- Created: 2016-05-16T05:12:45.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2025-01-24T10:06:26.000Z (about 1 year ago)
- Last Synced: 2025-02-16T09:43:06.531Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 45.9 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MJ
`Make JSON` from the command line.
The program takes a list of key=value parameter on the command line:
```shell
$ mj foo=bar
{"foo":"bar"}
$ mj foo=bar baz=quux
{"baz":"quux","foo":"bar"}
```
The only requirement being that the keys be unique:
```shell
$ 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:
```shell
$ 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:
```shell
$ 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 `--`:
```shell
$ 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:
```shell
$ mj foo[]=abc foo[]=def
{"foo":["abc","def"]}
```
But the operation is not supported on deeply-nested objects yet:
```shell
$ 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:
```shell
$ mj foo=@bar.txt
{"foo":"@bar.txt"}
```
However, you can designate certain prefixes to read values from a file:
```shell
$ echo hello-world > bar.txt
$ mj -r=@ foo=@bar.txt
{"foo":"hello-world\n"}
```