Merge json files using jq (one input object per file -> one larger output object, not a list)

谁说胖子不能爱 提交于 2021-02-05 09:27:38

问题


I am merging two json files using "jq -s . file1 file2", but I want them to get merged without comma separation. Also it shouldn't start with []

file 1:

{
  "node1": {
    "Environment": "PRD",
    "OS": "linux"
  },
  "node2": {
    "Environment": "NPR",
    "OS": "linux"
  }
}

file 2:

{
  "node3": {
    "Environment": "PRD",
    "OS": "linux"
  },
  "node4": {
    "Environment": "NPR",
    "OS": "linux"
  }
}

Output using jq -s . file 1 file 2

[
    {
      "node1": {
        "Environment": "PRD",
        "OS": "linux"
      },
      "node2": {
        "Environment": "NPR",
        "OS": "linux"
      }
    },
    {
      "node3": {
        "Environment": "PRD",
        "OS": "linux"
      },
      "node4": {
        "Environment": "NPR",
        "OS": "linux"
      }
    }
]

Required output:

{
  "node1": {
    "Environment": "PRD",
    "OS": "linux"
  },
  "node2": {
    "Environment": "NPR",
    "OS": "linux"
  },
    "node3": {
    "Environment": "PRD",
    "OS": "linux"
  },
  "node4": {
    "Environment": "NPR",
    "OS": "linux"
  }
} 

Can anyone help me finding solution to this, Thank you!


回答1:


One option slong the lines of your attempt:

jq -s add file1 file2

Another:

   jq -n 'input+input' file1 file2


来源:https://stackoverflow.com/questions/61769820/merge-json-files-using-jq-one-input-object-per-file-one-larger-output-object

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