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?
Figured it out.
You need to use
--jlistwrap.So the full command becomes:
mlr --icsv --ojson --jlistwrap cat sample.csv > sample.jsonWhich outputs this:
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: