Search code examples
jsoncsvmiller

MLR tool to convert JSON to CSV and pad missing fields from all possible headers in json


If I have three json objects in : test.json

{
  "queue": "A",
  "field1": 22,
  "field2": 2,
                 "unique_field": 0
}
{
  "queue": "B",
  "field1": 39,
  "field2": 3
}
{
  "queue": "C",
  "field1": 336,
  "field2": 5
}

Running MLR to convert to CSV returns two unique sets based on headers

./mlr --j2c cat test.json
queue,field1,field2,unique_field
A,22,2,0

queue,field1,field2
B,39,3
C,336,5

What I would like is for MLR to pad any missing fields with null so only 1 header is present

queue,field1,field2,unique_field
A,22,2,0
B,39,3,null
C,336,5,null

I am not sure what to try yet, as need expertise from MLR community


Solution

  • You must use the unsparsify verb:

    ./mlr --j2c unsparsify test.json