I have a text file that contains key value pairs in the format
Var1=Value1
Var2=Value2
Var3=Value3
I have converted them to variables using the below construct
while read -r line; do declare "$line"; done <inputfile.txt
I need to convert these variables or key / value pairs to a json format like below
[{
"Key": "Var1",
"Value": "Value1"
}, {
"Key": "Var2",
"Value": "Value2"
}, {
"Key": "Var3",
"Value": "Value3"
}]
How can I achieve the conversion to JSON form the variables, or reading directly from file without converting to variables, in a shell script?
I need to make it generic to read the key / value pairs from file and convert to JSON. I would then use the json string in a command, so the json needs to be on a single line as well. I just formatted it for readability, but need it to be
[{"Key": "Var1","Value": "Value1"}, {"Key": "Var2","Value": "Value2"}, {"Key": "Var3","Value": "Value3"}]
Thanks