0

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

adbdkb
  • 1,897
  • 6
  • 37
  • 66
  • [This similar question](https://stackoverflow.com/questions/51115902/convert-text-file-with-key-value-pair-to-specific-json-format-in-jq) should give some hints. – Shawn Apr 21 '20 at 01:28
  • Thanks @Shawn . But jq is not installed on the box and I may not be able to get it installed. Is there a pure shell script way to generate it? – adbdkb Apr 21 '20 at 01:34
  • 1
    @l0b0. I had already looked at that question before posting this. It has hardcoded keys, I need a generic format where the key names will also come from a file – adbdkb Apr 21 '20 at 02:02
  • The person who had answered it earlier, can you please repost your answer. The answer seems to have been removed when i was trying to explain why this is not duplicate. I would like to try the suggestion made earlier – adbdkb Apr 21 '20 at 02:07
  • Thanks, reopened. It's generally good to include such references in the question, to avoid closing. – l0b0 Apr 21 '20 at 02:07
  • 1
    @adbdkb, to implement a *correct* pure-shell answer, you need to implement the entirety of the JSON language in shell. That means escaping all the characters that need escaping, converting multi-byte Unicode characters to `\u....` sequences with the right codepoints, etc. – Charles Duffy Apr 21 '20 at 02:07
  • Is there a reason you aren't using the Python-embedded-in-bash answers available on several lookalikes/near-duplicates? Those will give you an answer *correct across all possible inputs*, which the presently-deleted answer (written by @tshiono) is not. – Charles Duffy Apr 21 '20 at 02:08
  • 2
    Basically it means reimplementing `jq` unless you really only have trivial keys and values. – l0b0 Apr 21 '20 at 02:08
  • @l0b0 - Yes, I have only pure alphanumeric characters for keys and values. Did the answer get deleted because it got marked duplicate or the person who posted the answer deleted it because it was marked duplicate? – adbdkb Apr 21 '20 at 02:10
  • @adbdkb, ...okay, if your values don't have tabs, don't have literal newlines, don't have quote characters, etc., then you might be able to get away with a string-concatenation-based answer. But don't forget to mention in your code's comments that it'll need to be *replaced* with something using jq or python if those conditions ever change -- it's easy to have bugs because something implemented when a set of conditions held continues to be reused even after they're no longer applicable. – Charles Duffy Apr 21 '20 at 02:11
  • @adbdkb, the answer was voluntarily deleted by the person who posted it. Why did that is a question only they can answer. – Charles Duffy Apr 21 '20 at 02:12
  • @CharlesDuffy - I will definitely put comments in my code, once either I figure out how to do that with pure shell commands or if the person reposts the answer that I could try and see if that works – adbdkb Apr 21 '20 at 02:16
  • `jq` is the de facto standard tool for working with json in scripts and from a command line. Not wanting to use it is just shooting yourself in the foot. The next best thing is a perl or python or whatever script using a json parser module. – Shawn Apr 21 '20 at 02:16
  • @Shawn - It is not that I don't want to use it. I would definitely want to learn to use it effectively, but it is currently not installed on the server, and i do not know, if the request will be honoured at this time. It may take a while to get that through – adbdkb Apr 21 '20 at 02:19

1 Answers1

0

Warning: do not use this code anywhere. Using Bash to create or consume JSON is just all round a really bad idea. If you don't know why have a look at a similar question for HTML. You have been warned.

With purely alphanumeric keys and values you can do something like this:

printf '['
while IFS='=' read -r key value
do
    printf '{"%s":"%s"},' "$key" "$value"
done < inputfile.txt | sed 's/.$//'
printf ']'
l0b0
  • 55,365
  • 30
  • 138
  • 223