jehiah / json2csv

command line tool to convert json to csv

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSON file

mwallach opened this issue · comments

commented

I have json file that looks like below. json2csv works if I make this each entry a single line. Is this format it fails. I there a way for json2csv handle file formatted in this way?
{
“count”:10,
“entries”:
[
{
“id”:20232913,
“application”:“AuditExampleLogin1”,
“user”:“TestUser”,
“time”:“2018-08-15T23:59:40.186-07:00”,
“values”:
null
},
{
“id”:20232914,
“application”:“AuditExampleLogin1”,
“user”:“AUser”,
“time”:“2018-08-15T23:59:55.186-07:00”,
“values”:
null
},

You can convert the entries array to produce a JSONL file (or stream) and then pass it to json2csv.

For example, given the following json

sample.json

{
  "count":10,
  "entries":
  [
    {
      "id":20232913,
      "application":"AuditExampleLogin1",
      "user":"TestUser",
      "time":"2018-08-15T23:59:40.186-07:00",
      "values":
      null
    },
    {
      "id":20232914,
      "application":"AuditExampleLogin1",
      "user":"AUser",
      "time":"2018-08-15T23:59:55.186-07:00",
      "values":
      null
    }
  ]
}

You could create the JSONL file with jq:

< sample.json jq -c '.entries[] | { id, application, user, time }' > sample.jsonl

Then you can use json2csv to convert it to a CSV:

< sample.jsonl json2csv -p -k id,application,user,time > sample.csv