Skip to content

Instantly share code, notes, and snippets.

@nilium
Last active December 28, 2018 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nilium/10e561141a1e09af4e9434ba459f0ca4 to your computer and use it in GitHub Desktop.
Save nilium/10e561141a1e09af4e9434ba459f0ca4 to your computer and use it in GitHub Desktop.
A few shell scripts for creating JSON from a shell
#!/usr/bin/env bash
while [ $# -gt 0 ]; do
jq -nc --arg k "$1" --arg v "$2" '{($k): (try ($v|fromjson) catch $v)}'
shift; shift
done | jq -sc add

The following command can be used to construct a simple Marathon application definition:

$ kv                                              \
    id python-server                              \
    cmd 'python3 -m http.server 8080'             \
    cpus 0.1                                      \
    mem 64                                        \
    instances 1                                   \
    container "$(kv                               \
        type DOCKER                               \
        docker "$(kv image python:3)"             \
        portMappings "$(list                      \
            "$(kv containerPort 8080 hostPort 0)" \
        )"                                        \
    )" |
    jq . # kv/list/etc. compact output -- pretty print this

This produces the following output (including the pretty-printing above):

{
  "id": "python-server",
  "cmd": "python3 -m http.server 8080",
  "cpus": 0.1,
  "mem": 64,
  "instances": 1,
  "container": {
    "type": "DOCKER",
    "docker": {
      "image": "python:3"
    },
    "portMappings": [
      {
        "containerPort": 8080,
        "hostPort": 0
      }
    ]
  }
}
#!/usr/bin/env bash
while [ $# -gt 0 ]; do
jq -nc --arg v "$1" '[(try ($v|fromjson) catch $v)]'
shift
done | jq -sc add
#!/usr/bin/env bash
exec jq -nc --arg v "$*" '$v'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment