I have a plain text file in format like this:
foo: bar
color: blue
names: joe, john, mary, bob, sue
I need to output the contents of this file as a JSON format:
{
"foo": "bar",
"color": "blue",
"names": ["joe", "john", "mary", "bob", "sue"]
}
I have tried using: jq -R -n '[inputs|split(":")|[{(.0):.[1] | add' testfile
(let's say the name of the file is testfile) but I can only get this result:
{
"foo": "bar",
"color": "blue",
"names": "joe, john, mary, bob, sue"
}
What do I need to do in order to get the desired output? I've tried piping split(",")
after [1]
but it makes every value pair into an array, which I don't want.
question from:
https://stackoverflow.com/questions/65832707/using-jq-to-convert-plain-text-block-to-json 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…