Trying to read .env file and convert it into to json file.
Input:
os=amazon-linux
region=ap-south-1
Desired ouput:
{
"os": "amazon-linux",
"region": "ap-south-1"
}
I am trying to use jq but getting 2 problems –
- Handle to empty lines –
jq: error (at .env:1): null (null) only strings can be parsed - Handle commented line –
#this is comment=>"#this is comment": null
Note: .env file is not nested value file
>Solution :
You could use capture to grab the parts by regular expressions (lines not matching would be ignored altogether), and from_entries to construct the object:
jq -Rn '[inputs | capture("(?<key>[^=]+)=(?<value>.*)")] | from_entries'
{
"os": "amazon-linux",
"region": "ap-south-1"
}