title: How to merge identically structured, nested json files using jq
body: <p>I need to merge an array in a series of identically structured, nested JSON files that share the same higher level keys. </p>
  
  <p>The goal is to create a merged file while retaining all of the existing, higher level keys and values. </p>
  
  <p><strong>File 1:</strong> </p>
  
  <pre><code>{
    "account": "123456789012",
    "regions": [
      {
        "region": "one",
        "services": [
          {
            "groups": [
              {
                "GroupId": "123456",
                "GroupName": "foo"
              },
              {
                "GroupId": "234567",
                "GroupName": "bar"
              }
            ]
          }
        ]
      }
    ]
  }
  </code></pre>
  
  <p><strong>File 2:</strong></p>
  
  <pre><code>{
    "account": "123456789012",
    "regions": [
      {
        "region": "one",
        "services": [
          {
            "group_policies": [
              {
                "GroupName": "foo",
                "PolicyNames": [
                  "all_foo",
                  "all_bar"                
                ]
              },
              {
                "GroupName": "bar",
                "PolicyNames": [
                  "all_bar"
                ]
              }
            ]
          }
        ]
      }
    ]
  }
  </code></pre>
  
  <p><strong>Expected result:</strong></p>
  
  <pre><code>{
    "account": "123456789012",
    "regions": [
      {
        "region": "one",
        "services": [
          {
            "groups": [
              {
                "GroupId": "123456",
                "GroupName": "foo"
              },
              {
                "GroupId": "234567",
                "GroupName": "bar"
              }
            ]
          },
          {
            "group_policies": [
             {
                "GroupName": "foo",
                "PolicyNames": [
                  "all_foo",
                  "all_bar"                
                ]
              },
              {
                "GroupName": "bar",
                "PolicyNames": [
                  "all_bar"
                ]
              }
             ]
          }
        ]
      }
    ]
  }
  </code></pre>
  
  <p>I have tried the following based on answers to other questions of this type with no success: </p>
  
  <pre><code>jq -s '.[0] * .[1]' test1.json test2.json
  
  jq -s add test1.json test2.json
  
  jq -n '[inputs[]]' test{1,2}.json
  </code></pre>
  
  <p>The following successfully merges the array but lacks the higher level keys and values in the results.</p>
  
  <pre><code>jq -s '.[0].regions[0].services[0] * .[1].regions[0].services[0]' test1.json test2.json
  </code></pre>
  
  <p>I am assuming there is a simple jq solution to this that is escaping my searches. If not, any combination of jq and bash will work for a solution. </p>