Skip to main content
3 of 3
deleted 30 characters in body
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

Using any awk in any shell on every Unix box:

$ cat tst.awk BEGIN { RS = "" FS = "\n" printf "[" } { gsub(/"/,"\\\\&") title = $1 body = $3 for (i=4; i<NF; i++) { body = body "\\n" $i } print (NR>1 ? "," : "") print " {" printf " \"title\": \"%s\",\n", title printf " \"body\": \"%s\"\n", body printf " }" } END { print "\n]" } 

$ awk -f tst.awk file [ { "title": "A random Title 1", "body": "1- a block of text that can contain any character\nand it also can contain multiple lines" }, { "title": "A random Title 2", "body": "2- a block of text that can contain any character\nand it also can contain multiple lines" }, { "title": "A random Title 3", "body": "3- a block of text that can contain any character\nand it also can contain multiple lines" } ] 
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60