Categories
json linux shell

how to add json object to json file using shell script

json file as follows:

{"name" :"sam",
"age":23,
"designation":"doctor"}

now i want to add another json object {“location”:”canada”} at the end of the file using bash script
i have tried echo “{“location”:”canada”}”>>sample.json

but it results

{"name" :"sam",
"age":23,
"designation":"doctor"} {location:canada}

but i want it to be like this

{"name" :"sam",
"age":23,
"designation":"doctor",
"location":"canada"}

please suggest me

sed -i '$s/}/,\n"location":"canada"}/' sample.json

Result:

{"name" :"sam",
"age":23,
"designation":"doctor",
"location":"canada"}