Skip to content

Instantly share code, notes, and snippets.

@eiri
Created May 8, 2018 13:53
Show Gist options
  • Save eiri/b1f1a79e421711861df6d665c1b5a4f4 to your computer and use it in GitHub Desktop.
Save eiri/b1f1a79e421711861df6d665c1b5a4f4 to your computer and use it in GitHub Desktop.
jo and jg comparison and cheatsheet

jo and jg comparison and cheatsheet

What is it?

Both command line utilities allow to JSON output from a shell.

Links

Basics

jo string=name integer=1457081292 float=12.3456 float2=1e-5 nil= boolean=true
{"string":"name","integer":1457081292,"float":12.3456,"float2":1e-05,"nil":null,"boolean":true}

Note that jo preserves key order

jg string=name integer=1457081292 float=:12.3456 float2=:1e-5 nil=:null boolean=:true
{"boolean":true,"float":12.3456,"float2":1e-05,"integer":"1457081292","nil":null,"string":"name"}

Note that jg sorts keys alphabetically

Keys merging

jo keeps all the keys (and generates invalid JSON as per rfc4627 The names within an object SHOULD be unique.)

jo a=1 a=2 a=3
{"a":1,"a":2,"a":3}

jg merge keys and last key wins (it's the same what jq does parsing JSON)

jg a=1 a=2 a=3
{"a":"3"}

Arrays

jo -a 1 2 3
[1,2,3]

seq 3 | jo -a
[1,2,3]

jo array[]=1 array[]=2 array[]=3
{"array":[1,2,3]}
jg [:1 :2 :3]
[1,2,3]

jg array=[:1 :2 :3]
{"array":[1,2,3]}

Array of objects

jo -a $(jo id=1) $(jo id=2)
[{"id":1},{"id":2}]
jg [id=:1 id=:2]
[{"id":1},{"id":2}]

Deep objects

jo a=$(jo a=1 b=2) b=$(jo a=3 b=4)
{"a":{"a":1,"b":2},"b":{"a":3,"b":4}}

jo a[a]=1 a[b]=2 b[a]=3 b[b]=4
{"a":{"a":1,"b":2},"b":{"a":3,"b":4}}
jg a={a=:1 b=:2} b={a=:3 b=:4}
{"a":{"a":1,"b":2},"b":{"a":3,"b":4}}

jg a.a=:1 a.b=:2 b.a=:3 b.b=:4
{"a":{"a":1,"b":2},"b":{"a":3,"b":4}}

jg a.b.c=value
{"a":{"b":{"c":"value"}}}

jo and booleans

jo bool@T
{"bool":true}
jo bool@F
{"bool":false}

jo bool@t
{"bool":true}
jo bool@f
{"bool":false}

jo bool@1
{"bool":true}
jo bool@0
{"bool":false}

jo and reading from files

cat a
eiri@eiri.ca

jo email=@a encoded=%a
{"email":"eiri@eiri.ca","encoded":"ZWlyaUBlaXJpLmNhCg=="}

jg and multiline object or arrays

jg {key=a} {key=b}
{"key":"a"}
{"key":"b"}

jg [key=a] [key=b]
[{"key":"a"}]
[{"key":"b"}]

Summary

jo is more convinient when you need to work with files and pipes. jq works better with deep objects and multiline JSON

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment