3

Using the Miller command line tool I want to convert a CSV file with headers into a JSON array.

Currently I am using this command: mlr --icsv --ojson cat sample.csv > sample.json

It is outputting JSON, but not in array format.

This is the sample CSV input:

Keyword, Weight, Quantity
Apple, 10, 2345
Orange, 23, 467
Banana, 2345, 2345

And this is the output I am getting from Miller:

{ "Keyword": "Apple", "Weight": 10, "Quantity": 2345 }
{ "Keyword": "Orange", "Weight": 23, "Quantity": 467 }
{ "Keyword": "Banana", "Weight": 2345, "Quantity": 2345 }

As you can see this output is JSON Lines, not an array format.

I want the JSON to be an array, like this:

[
    { "Keyword": "Apple", "Weight": 10, "Quantity": 2345 },
    { "Keyword": "Orange", "Weight": 23, "Quantity": 467 },
    { "Keyword": "Banana", "Weight": 2345, "Quantity": 2345 }
]

What is the correct Miller command for that?

TinyTiger
  • 1,801
  • 7
  • 47
  • 92

1 Answers1

3

Figured it out.

You need to use --jlistwrap.

So the full command becomes: mlr --icsv --ojson --jlistwrap cat sample.csv > sample.json

Which outputs this:

[
{ "Keyword": "Apple", "Weight": 10, "Quantity": 2345 }
,{ "Keyword": "Orange", "Weight": 23, "Quantity": 467 }
,{ "Keyword": "Banana", "Weight": 2345, "Quantity": 2345 }
]

It's not formatted beautifully (commas on the wrong line, and not indented) but it's a valid JSON array.

After running through a tool to auto-format the JSON it would look like this:

[
   {
      "Keyword":"Apple",
      "Weight":10,
      "Quantity":2345
   },
   {
      "Keyword":"Orange",
      "Weight":23,
      "Quantity":467
   },
   {
      "Keyword":"Banana",
      "Weight":2345,
      "Quantity":2345
   }
]
TinyTiger
  • 1,801
  • 7
  • 47
  • 92