Skip to content

Instantly share code, notes, and snippets.

@danchoi
Last active August 29, 2015 14:10
Show Gist options
  • Save danchoi/f162d2a4e4aae7d38e1d to your computer and use it in GitHub Desktop.
Save danchoi/f162d2a4e4aae7d38e1d to your computer and use it in GitHub Desktop.
jsontsv vs json2csv

jsontsv vs json2csv

json2csv accepts only compact JSON input; jsontsv can accept pretty-formatted JSON input:

Input 1:

{"title":"Terminator 2: Judgment Day","year":1991,"stars":[{"name":"Arnold Schwarzenegger"},{"name":"Linda Hamilton"}],"ratings":{"imdb":8.5}}
{"title":"Interstellar","year":2014,"stars":[{"name":"Matthew McConaughey"},{"name":"Anne Hathaway"}],"ratings":{"imdb":8.9}}

Input 2:

{
  "title": "Terminator 2: Judgement Day",
  "year": 1991,
  "stars": [
    {
      "name": "Arnold Schwarzenegger"
    },
    {
      "name": "Linda Hamilton"
    }
  ],
  "ratings": {
    "imdb": 8.5
  }
}
{
  "title": "Interstellar",
  "year": 2014,
  "stars": [
    {
      "name": "Matthew McConaughey"
    },
    {
      "name": "Anne Hathaway"
    }
  ],
  "ratings": {
    "imdb": 8.9
  }
}
json2csv -k title,year,ratings.imdb  < input1   # works
json2csv -k title,year,ratings.imdb < input2   # FAILS

jsontsv 'title year ratings.imdb' < input1  # works
jsontsv 'title year ratings.imdb' < input2  # works

jsontsv can reach into objects in an array, json2csv is more limited

json2csv -k stars.name < input   
""
""

jsontsv 'stars.name' < input  
Arnold Schwarzenegger,Linda Hamilton
Matthew McConaughey,Anne Hathaway

jsontsv 'stars[0].name' < input  
Arnold Schwarzenegger
Matthew McConaughey

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