Scooby Scooby - 1 year ago 62
Bash Question

Merge two json in bash (no jq)

I have two jsons :

env.json

{
"environment":"INT",
"3scale":{
"provider-key":>"dee9b91dc184afdc8adb6fa79f9ede11",
"admin-domain":"https://dow-jones-oauth-admin.3scale.net",
"phoneHomeIntervalInSecs":"1800"
},
"newrelic":{
"application_monitoring":{
"appname":"3Scale-Int"
},
"license":"fce89cd508bef3a6978c1d75b2b1f70d497f5885"
}
}


roles.json

{
"run_list":[
"recipe[splunk-dj]",
"recipe[tideway]",
"recipe[AlertsSearch::newrelic]",
"recipe[AlertsSearch]"
]
}


expected output should be some thing like this :

{
"environment":"INT",
"3scale":{
"provider-key":>"dee9b91dc184afdc8adb6fa79f9ede11",
"admin-domain":"https://dow-jones-oauth-admin.3scale.net",
"phoneHomeIntervalInSecs":"1800"
},
"newrelic":{
"application_monitoring":{
"appname":"3Scale-Int"
},
"license":"fce89cd508bef3a6978c1d75b2b1f70d497f5885"
},
"run_list":[
"recipe[splunk-dj]",
"recipe[tideway]",
"recipe[AlertsSearch::newrelic]",
"recipe[AlertsSearch]"
]
}


I need to merge these two json (and other like these two) into one single json using only available inbuilt bash commands.

only have sed, cat, echo, tail, wc at my disposal.

Answer Source

A little bit hacky, but hopefully will do.

env_lines=`wc -l < $1`
env_output=`head -n $(($env_lines - 1)) $1`
roles_lines=`wc -l < $2`
roles_output=`tail -n $(($roles_lines - 1)) $2`
echo "$env_output" "," "$roles_output"