0

I am trying to merge two json files using command "jq -s . file1 file2". But I want these file's content to be comma separated, neither it should 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 please help in finding the solution. Thanks

1
1
$ jq -s 'add' file[12].json
{
  "node1": {
    "Environment": "PRD",
    "OS": "linux"
  },
  "node2": {
    "Environment": "NPR",
    "OS": "linux"
  },
  "node3": {
    "Environment": "PRD",
    "OS": "linux"
  },
  "node4": {
    "Environment": "NPR",
    "OS": "linux"
  }
}

Using add will concatenate the top-level keys in the list that is slurped in from the files, creating a merged object of those keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.