Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert "=" separated values file to ":" json file

Trying to read .env file and convert it into to json file.

Input:

os=amazon-linux
region=ap-south-1

Desired ouput:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
"os": "amazon-linux",
"region": "ap-south-1"
}

I am trying to use jq but getting 2 problems –

  1. Handle to empty lines – jq: error (at .env:1): null (null) only strings can be parsed
  2. 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"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading