jsonmacosshellsed

How can I sed two curly braces on two lines and add a coma between them on a Mac?


I'm trying to parse some JSON and am running into an issue with parsing. My objects are:

{ 
  "foo" : "bar"
}
{
  "spam" : "ham"
}

I want to change this in a shell script to:

{ 
  "foo" : "bar"
},
{
  "spam" : "ham"
}

I've tried:

sed 's/}\n{/},\n{/g' file.json > file.json.tmp

and solutions like Replace curly braces separated by a newline with sed But without any progress. Any clue what I am missing here?


Solution

  • Using perl is easier :

    perl -0777 -pe 's/}\n\{/},\n{/g' file.json > file.json.tmp
    

    Or if you have gnu-sed :

    sed ':a;N;$!ba;s/}\n[{]/},\n{/g' file.json > file.json.tmp