Merge two json in bash (no jq)

一个人想着一个人 提交于 2019-12-08 14:20:35

问题


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.


回答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"



回答2:


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


来源:https://stackoverflow.com/questions/38659763/merge-two-json-in-bash-no-jq

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!