0

I have two jsons :

env.json

{  
   "environment":"INT"
}

roles.json

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

expected output should be some thing like this :

{  
       "environment":"INT",
    "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.

Scooby
  • 3,371
  • 8
  • 44
  • 84

3 Answers3

4

Tell whoever put the constraint "bash only" on the project that bash is not sufficient for processing JSON, and get jq.

$ jq --slurp 'add' env.json roles.json
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 4
    If processing JSON is important, get the right tools to process it. – chepner Jul 29 '16 at 14:07
  • 2
    Yeah, I love those "well, you shouldn't do that, so go fire your managers and team leads and senior team members so that somebody from outside the company can tell them how to do things" answers. So helpful. – Keith Tyler Aug 01 '16 at 16:35
0

I couldn't use jq either as I was limited due to client's webhost jailing the user on the command line with limited binaries as most discount/reseller web hosting companies do. Luckily they usually have PHP available and you can do a oneliner command like this which something like what I would place in my install/setup bash script for example.

php -r '$json1 = "./env.json";$json2 = "./roles.json";$data = array_merge(json_decode(file_get_contents($json1), true),json_decode(file_get_contents($json2),true));echo json_encode($data, JSON_PRETTY_PRINT);'

For clarity php -r accepts line feeds as well so using this also works.

php -r '
$json1 = "./env.json";
$json2 = "./roles.json";
$data = array_merge(json_decode(file_get_contents($json1), true), json_decode(file_get_contents($json2), true));
echo json_encode($data, JSON_PRETTY_PRINT);'

Output

{
    "environment": "INT",
    "run_list": [
        "recipe[splunk-dj]",
        "recipe[tideway]",
        "recipe[AlertsSearch::newrelic]",
        "recipe[AlertsSearch]"
    ]
}
Anthony Hatzopoulos
  • 10,437
  • 2
  • 40
  • 57
-1

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"
asenovm
  • 6,397
  • 2
  • 41
  • 52