Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I _love_ jq. It's been an incredibly useful tool for me since I discovered it ~6 months ago. However, article mentions "jq -n"; I personally find jq syntax less appealing when it comes to generating JSON instead of parsing it. For that particular task, I prefer using "jo":

https://github.com/jpmens/jo

jq & jo are the Calvin & Hobbes of the shell: lots of fun together!

A few examples:

- Generate a simple JSON object:

  $ jo foo=12 bar=true baz=null
  {"foo":12,"bar":true,"baz":"null"}
- Want a nested object?

  $ jo foo=12 bar=true baz=null thing=$(jo number=57 test=string)
  {"foo":12,"bar":true,"baz":"null","thing":{"number":57,"test":"string"}}
- What about arrays?

  $ jo thing=$(jo number=57 test=string) anarray=$(jo -a 1 2 3 4 5)
  {"thing":{"number":57,"test":"string"},"anarray":[1,2,3,4,5]}
- Now let's add some jq magic to sum all the values in the array together:

  $ jo thing=$(jo number=57 test=string) anarray=$(jo -a 1 2 3 4 5) | jq '.anarray | add'
  15
Great stuff :)


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: