Create JSON output from the Linux command line


This past weekend while working on create-seccomp-profile I needed a way to generate JSON output from arbitrary text. After a little googling I came across the incredibly useful jo utility. Where jq is invaluable for pulling arbitrary data out of a JSON data structure jo is amazing at creating JSON structures. In it’s simplest form jo can take key value pairs and produce pretty printed JSON output:

$ jo -p name=shibby array=$(jo -a foo=1 bar=2)

{
   "name": "shibby",
   "array": [
      "foo=1",
      "bar=2"
   ]
}

Jo also has a number of other capabilities to create arrays and complex object hierarchies:

$ jo -p defaultAction=SCMP_ACT_ERRNO architectures=$(jo -a SCMP_ARCH_X86_64 SCMP_ARCH_X86 SCMP_ARCH_X32) sycalls=$(jo -a $(jo name=read action=accept) $(jo name=write action=accept args=[]))

{
   "defaultAction": "SCMP_ACT_ERRNO",
   "architectures": [
      "SCMP_ARCH_X86_64",
      "SCMP_ARCH_X86",
      "SCMP_ARCH_X32"
   ],
   "sycalls": [
      {
         "name": "read",
         "action": "accept"
      },
      {
         "name": "write",
         "action": "accept",
         "args": []
      }
   ]
}

In teh example above I created a simple docker seccomp profile. This is a super useful utility and I hope it gets rolled out to all of the major distributions. It’s in Ubuntu 17.10 so life is grand. :)

This article was posted by Matty on 2017-11-26 14:46:17 -0500 -0500