0

Let’s say I have a

file
with the following content:

var1='a random text'
var2='another random text'
var3='a third random text'

I know that if I use the command

eval
like the following I’ll store all those variables directly on my shell:

eval(cat file)

Doing that, my shell will create

var1</pre></div><!-- /wp:codemirror-blocks/code-block -->, <!-- wp:codemirror-blocks/code-block {"showPanel":false,"languageLabel":"no","mode":"clike","mime":"text\/x-c++src"} --> 				<div class="wp-block-codemirror-blocks-code-block code-block"><pre>var2
and
var3</pre></div><!-- /wp:codemirror-blocks/code-block --> with their respective contents. Knowing that, I could generate a JSON manually like the following:<!-- /wp:paragraph -->  <!-- wp:codemirror-blocks/code-block {"showPanel":false,"languageLabel":"no","mode":"clike","mime":"text\/x-c++src"} --> 				<div class="wp-block-codemirror-blocks-code-block code-block"><pre> JSON="{ "var1" : "var1", "var2" : "var2", "var3" : "var3"}" </pre></div><!-- /wp:codemirror-blocks/code-block -->  <!-- wp:paragraph -->And that would result in a valid JSON:<!-- /wp:paragraph -->  <!-- wp:codemirror-blocks/code-block {"showPanel":false,"languageLabel":"no","mode":"clike","mime":"text\/x-c++src"} --> 				<div class="wp-block-codemirror-blocks-code-block code-block"><pre> echo $JSON
{ "var1" : "a random text", "var2" : "another random text", "var3" : "a third random text"}

The problem here is that I’m hardcoding the keys var1, var2 and var3… In my case, the file could be bigger and with more variables stored in it (not just var1, var2 and var3). I was thinking if there was an easy way of achieving that using the command line, just like

eval
does for storing file variables on the shell, but instead of storing the variables, generating a JSON output. Is it possible? Can I directly convert a file structured like that to JSON using the command line?


My alternative solution here would be developing a code (not using shell) that goes char by char inside this file and then I separate everything dynamically in a loop. But I’m making this question because I want to avoid overcomplicating the solution.