1

I want output in the below JSON array format with command on linux/bash platform . Can anybody help

data in text file

test:test
test1:test1
test4:test4

Expecting output:

{array : 
   [
     {test:test},
     {test1:test1}, 
     {test4:test4}
   ]
}
project_linux
  • 11
  • 1
  • 4
  • How was this data generated? – bob dylan Mar 03 '20 at 09:47
  • 1
    Note that `”` is not `"`. Just read the input line by line with `:` as separator and output them in the formar `{"something":"something"}` then joni the lines with comma. [How to read a file line by line](https://mywiki.wooledge.org/BashFAQ/001) [How to join lines with delimeter](https://stackoverflow.com/questions/2764051/how-to-join-multiple-lines-of-file-names-into-one-with-custom-delimiter) – KamilCuk Mar 03 '20 at 10:00
  • 1
    Your output is not valid JSON... – Shawn Mar 03 '20 at 10:04
  • I have made changes in the question. Please check if that makes more sense – project_linux Mar 03 '20 at 10:10
  • Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the [help] and read [ask], and especially read [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – kvantour Mar 03 '20 at 10:12
  • 1
    Your expected output is still not valid JSON. If you want something JSON-like but not, don't call it JSON. – Shawn Mar 03 '20 at 10:24

3 Answers3

4

Using jq command line JSON parser:

<file jq -Rs '{array:split("\n")|map(split(":")|{(.[0]):.[1]}?)}'
{
  "array": [
    {
      "test": "test"
    },
    {
      "test1": "test1"
    },
    {
      "test4": "test4"
    }
  ]
}

The options Rs let jq read the whole file as one string.

The script splits this string into pieces in order to have the expected format.

oliv
  • 12,690
  • 25
  • 45
  • Can I append new pair (Key/value) to the existing array "test5": "test5" { "array": [ { "test": "test" }, { "test1": "test1" }, { "test4": "test4" }, { "test5”: "test5” } ] } – project_linux Mar 03 '20 at 14:16
0

Assuming you want actual JSON output:

$ jq -nR '{array: (reduce inputs as $line ([]; . + [$line | split(":") | {(.[0]):.[1]}]))}' input.txt 
{
  "array": [
    {
      "test": "test"
    },
    {
      "test1": "test1"
    },
    {
      "test4": "test4"
    }
  ]
}
Shawn
  • 47,241
  • 3
  • 26
  • 60
0

Creating a JSON works best with a dedicated JSON tool that can also make sense of raw text.
xidel is such a tool.

XPath:

xidel -s input.txt -e '{"array":x:lines($raw) ! {substring-before(.,":"):substring-after(.,":")}}'

(x:lines($raw) is a shorthand for tokenize($raw,'\r\n?|\n'), which creates a sequence of all lines.)

XQuery:

xidel -s input.txt --xquery '{"array":for $x in x:lines($raw) let $a:=tokenize($x,":") return {$a[1]:$a[2]}}'

See also this Xidel online tester.

Reino
  • 3,203
  • 1
  • 13
  • 21