Merge two JSON documents together












0















I am trying append some JSON to existing ~/.docker/config.json document using Ansible.



Document 1 ~/.docker/config.json



{
"auths": {
"https://index.docker.io/v1/": {
"auth": "supercalifragilistic"
}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/18.03.0-ce (windows)"
}
}


Document 2 credHelper.json



{
"credHelpers": {
"111111111111.dkr.ecr.us-east-1.amazonaws.com" : "ecr-login"
}
}


I have been spending some time with jq but cannot get it to work. This is the result of jq.



jq -s '.' credHelpers.json config.json



[
{
"credHelpers": {
"111111111111.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
}
},
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "supercalifragilistic"
}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/18.03.0-ce (windows)"
}
}
]


What I want is this:



{
"auths": {
"https://index.docker.io/v1/": {
"auth": "supercalifragilistic"
}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/18.03.0-ce (windows)"
},
"credHelpers": {
"111111111111.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
}
}


I Hope I can make this idempotent, too, so the merging/joining doesn't keep happening recursively each time playbook is run.










share|improve this question





























    0















    I am trying append some JSON to existing ~/.docker/config.json document using Ansible.



    Document 1 ~/.docker/config.json



    {
    "auths": {
    "https://index.docker.io/v1/": {
    "auth": "supercalifragilistic"
    }
    },
    "HttpHeaders": {
    "User-Agent": "Docker-Client/18.03.0-ce (windows)"
    }
    }


    Document 2 credHelper.json



    {
    "credHelpers": {
    "111111111111.dkr.ecr.us-east-1.amazonaws.com" : "ecr-login"
    }
    }


    I have been spending some time with jq but cannot get it to work. This is the result of jq.



    jq -s '.' credHelpers.json config.json



    [
    {
    "credHelpers": {
    "111111111111.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
    }
    },
    {
    "auths": {
    "https://index.docker.io/v1/": {
    "auth": "supercalifragilistic"
    }
    },
    "HttpHeaders": {
    "User-Agent": "Docker-Client/18.03.0-ce (windows)"
    }
    }
    ]


    What I want is this:



    {
    "auths": {
    "https://index.docker.io/v1/": {
    "auth": "supercalifragilistic"
    }
    },
    "HttpHeaders": {
    "User-Agent": "Docker-Client/18.03.0-ce (windows)"
    },
    "credHelpers": {
    "111111111111.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
    }
    }


    I Hope I can make this idempotent, too, so the merging/joining doesn't keep happening recursively each time playbook is run.










    share|improve this question



























      0












      0








      0








      I am trying append some JSON to existing ~/.docker/config.json document using Ansible.



      Document 1 ~/.docker/config.json



      {
      "auths": {
      "https://index.docker.io/v1/": {
      "auth": "supercalifragilistic"
      }
      },
      "HttpHeaders": {
      "User-Agent": "Docker-Client/18.03.0-ce (windows)"
      }
      }


      Document 2 credHelper.json



      {
      "credHelpers": {
      "111111111111.dkr.ecr.us-east-1.amazonaws.com" : "ecr-login"
      }
      }


      I have been spending some time with jq but cannot get it to work. This is the result of jq.



      jq -s '.' credHelpers.json config.json



      [
      {
      "credHelpers": {
      "111111111111.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
      }
      },
      {
      "auths": {
      "https://index.docker.io/v1/": {
      "auth": "supercalifragilistic"
      }
      },
      "HttpHeaders": {
      "User-Agent": "Docker-Client/18.03.0-ce (windows)"
      }
      }
      ]


      What I want is this:



      {
      "auths": {
      "https://index.docker.io/v1/": {
      "auth": "supercalifragilistic"
      }
      },
      "HttpHeaders": {
      "User-Agent": "Docker-Client/18.03.0-ce (windows)"
      },
      "credHelpers": {
      "111111111111.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
      }
      }


      I Hope I can make this idempotent, too, so the merging/joining doesn't keep happening recursively each time playbook is run.










      share|improve this question
















      I am trying append some JSON to existing ~/.docker/config.json document using Ansible.



      Document 1 ~/.docker/config.json



      {
      "auths": {
      "https://index.docker.io/v1/": {
      "auth": "supercalifragilistic"
      }
      },
      "HttpHeaders": {
      "User-Agent": "Docker-Client/18.03.0-ce (windows)"
      }
      }


      Document 2 credHelper.json



      {
      "credHelpers": {
      "111111111111.dkr.ecr.us-east-1.amazonaws.com" : "ecr-login"
      }
      }


      I have been spending some time with jq but cannot get it to work. This is the result of jq.



      jq -s '.' credHelpers.json config.json



      [
      {
      "credHelpers": {
      "111111111111.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
      }
      },
      {
      "auths": {
      "https://index.docker.io/v1/": {
      "auth": "supercalifragilistic"
      }
      },
      "HttpHeaders": {
      "User-Agent": "Docker-Client/18.03.0-ce (windows)"
      }
      }
      ]


      What I want is this:



      {
      "auths": {
      "https://index.docker.io/v1/": {
      "auth": "supercalifragilistic"
      }
      },
      "HttpHeaders": {
      "User-Agent": "Docker-Client/18.03.0-ce (windows)"
      },
      "credHelpers": {
      "111111111111.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
      }
      }


      I Hope I can make this idempotent, too, so the merging/joining doesn't keep happening recursively each time playbook is run.







      json ansible jq idempotent






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 4:54







      Felipe Alvarez

















      asked Jan 2 at 4:49









      Felipe AlvarezFelipe Alvarez

      2,53921928




      2,53921928
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Is this the code that you're looking for?



              - set_fact:
          doc1: "{{ lookup('file', 'config.json') }}"
          doc2: "{{ lookup('file', 'credHelper.json') }}"
          - debug: msg="{{ doc1 | combine(doc2) }}"



          TASK [debug] **********************************************************
          ok: [localhost] => {
          "msg": {
          "HttpHeaders": {
          "User-Agent": "Docker-Client/18.03.0-ce (windows)"
          },
          "auths": {
          "https://index.docker.io/v1/": {
          "auth": "supercalifragilistic"
          }
          },
          "credHelpers": {
          "111111111111.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
          }
          }
          }





          share|improve this answer
























          • Will try this one out, and make a decision soon.

            – Felipe Alvarez
            Jan 5 at 14:46



















          1














          Just to complete the picture, this is also possible with jq, simply add two json files:



          jq -s 'add' credHelpers.json config.json


          The add filter takes as input an array, and produces as output the elements of the array added together, as the jq documentation says. In addition you need the --slurp/-s command line option, which turns the entire input stream into a large array and runs the filter just once.






          share|improve this answer


























          • I like add. It works. Will try to use Ansible-native solution (fewer dependencies), and then I'll decide the victor.

            – Felipe Alvarez
            Jan 5 at 14:46











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001333%2fmerge-two-json-documents-together%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Is this the code that you're looking for?



              - set_fact:
          doc1: "{{ lookup('file', 'config.json') }}"
          doc2: "{{ lookup('file', 'credHelper.json') }}"
          - debug: msg="{{ doc1 | combine(doc2) }}"



          TASK [debug] **********************************************************
          ok: [localhost] => {
          "msg": {
          "HttpHeaders": {
          "User-Agent": "Docker-Client/18.03.0-ce (windows)"
          },
          "auths": {
          "https://index.docker.io/v1/": {
          "auth": "supercalifragilistic"
          }
          },
          "credHelpers": {
          "111111111111.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
          }
          }
          }





          share|improve this answer
























          • Will try this one out, and make a decision soon.

            – Felipe Alvarez
            Jan 5 at 14:46
















          1














          Is this the code that you're looking for?



              - set_fact:
          doc1: "{{ lookup('file', 'config.json') }}"
          doc2: "{{ lookup('file', 'credHelper.json') }}"
          - debug: msg="{{ doc1 | combine(doc2) }}"



          TASK [debug] **********************************************************
          ok: [localhost] => {
          "msg": {
          "HttpHeaders": {
          "User-Agent": "Docker-Client/18.03.0-ce (windows)"
          },
          "auths": {
          "https://index.docker.io/v1/": {
          "auth": "supercalifragilistic"
          }
          },
          "credHelpers": {
          "111111111111.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
          }
          }
          }





          share|improve this answer
























          • Will try this one out, and make a decision soon.

            – Felipe Alvarez
            Jan 5 at 14:46














          1












          1








          1







          Is this the code that you're looking for?



              - set_fact:
          doc1: "{{ lookup('file', 'config.json') }}"
          doc2: "{{ lookup('file', 'credHelper.json') }}"
          - debug: msg="{{ doc1 | combine(doc2) }}"



          TASK [debug] **********************************************************
          ok: [localhost] => {
          "msg": {
          "HttpHeaders": {
          "User-Agent": "Docker-Client/18.03.0-ce (windows)"
          },
          "auths": {
          "https://index.docker.io/v1/": {
          "auth": "supercalifragilistic"
          }
          },
          "credHelpers": {
          "111111111111.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
          }
          }
          }





          share|improve this answer













          Is this the code that you're looking for?



              - set_fact:
          doc1: "{{ lookup('file', 'config.json') }}"
          doc2: "{{ lookup('file', 'credHelper.json') }}"
          - debug: msg="{{ doc1 | combine(doc2) }}"



          TASK [debug] **********************************************************
          ok: [localhost] => {
          "msg": {
          "HttpHeaders": {
          "User-Agent": "Docker-Client/18.03.0-ce (windows)"
          },
          "auths": {
          "https://index.docker.io/v1/": {
          "auth": "supercalifragilistic"
          }
          },
          "credHelpers": {
          "111111111111.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 5:18









          Vladimir BotkaVladimir Botka

          1,7051410




          1,7051410













          • Will try this one out, and make a decision soon.

            – Felipe Alvarez
            Jan 5 at 14:46



















          • Will try this one out, and make a decision soon.

            – Felipe Alvarez
            Jan 5 at 14:46

















          Will try this one out, and make a decision soon.

          – Felipe Alvarez
          Jan 5 at 14:46





          Will try this one out, and make a decision soon.

          – Felipe Alvarez
          Jan 5 at 14:46













          1














          Just to complete the picture, this is also possible with jq, simply add two json files:



          jq -s 'add' credHelpers.json config.json


          The add filter takes as input an array, and produces as output the elements of the array added together, as the jq documentation says. In addition you need the --slurp/-s command line option, which turns the entire input stream into a large array and runs the filter just once.






          share|improve this answer


























          • I like add. It works. Will try to use Ansible-native solution (fewer dependencies), and then I'll decide the victor.

            – Felipe Alvarez
            Jan 5 at 14:46
















          1














          Just to complete the picture, this is also possible with jq, simply add two json files:



          jq -s 'add' credHelpers.json config.json


          The add filter takes as input an array, and produces as output the elements of the array added together, as the jq documentation says. In addition you need the --slurp/-s command line option, which turns the entire input stream into a large array and runs the filter just once.






          share|improve this answer


























          • I like add. It works. Will try to use Ansible-native solution (fewer dependencies), and then I'll decide the victor.

            – Felipe Alvarez
            Jan 5 at 14:46














          1












          1








          1







          Just to complete the picture, this is also possible with jq, simply add two json files:



          jq -s 'add' credHelpers.json config.json


          The add filter takes as input an array, and produces as output the elements of the array added together, as the jq documentation says. In addition you need the --slurp/-s command line option, which turns the entire input stream into a large array and runs the filter just once.






          share|improve this answer















          Just to complete the picture, this is also possible with jq, simply add two json files:



          jq -s 'add' credHelpers.json config.json


          The add filter takes as input an array, and produces as output the elements of the array added together, as the jq documentation says. In addition you need the --slurp/-s command line option, which turns the entire input stream into a large array and runs the filter just once.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 8:54

























          answered Jan 2 at 7:58









          JGKJGK

          1,2311513




          1,2311513













          • I like add. It works. Will try to use Ansible-native solution (fewer dependencies), and then I'll decide the victor.

            – Felipe Alvarez
            Jan 5 at 14:46



















          • I like add. It works. Will try to use Ansible-native solution (fewer dependencies), and then I'll decide the victor.

            – Felipe Alvarez
            Jan 5 at 14:46

















          I like add. It works. Will try to use Ansible-native solution (fewer dependencies), and then I'll decide the victor.

          – Felipe Alvarez
          Jan 5 at 14:46





          I like add. It works. Will try to use Ansible-native solution (fewer dependencies), and then I'll decide the victor.

          – Felipe Alvarez
          Jan 5 at 14:46


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001333%2fmerge-two-json-documents-together%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas