Using Miller (mlr
) to convert the header-less and space-delimited data into JSON:
$ mlr --n2j label name,street,phone,e-mail file
{ "name": "alex", "street": "street1", "phone": 253465873, "e-mail": "alex123@google.com" }
{ "name": "john", "street": "street2", "phone": 442893646, "e-mail": "3jj33@google.com" }
{ "name": "kevin", "street": "street3", "phone": 125234763, "e-mail": "ke1vn@yahoo.com" }
The output is a separate JSON object per input record. The --n2j
option is a shorthand for --inidx
("input is implicitly-integer-indexed (nidx)") in combination with --ojson
("output is JSON").
The label
sub-command renames the fields in order.
Would you want to wrap the set of generated objects in an array, use --jlistwrap
:
$ mlr --n2j --jlistwrap label name,street,phone,e-mail file
[
{ "name": "alex", "street": "street1", "phone": 253465873, "e-mail": "alex123@google.com" }
,{ "name": "john", "street": "street2", "phone": 442893646, "e-mail": "3jj33@google.com" }
,{ "name": "kevin", "street": "street3", "phone": 125234763, "e-mail": "ke1vn@yahoo.com" }
]