Increment value and save back to the file












1















I have a simple JSON file like this,



{

...
"version": "2.1.0",
"buildNumber": 83

}


I want to simply increment the buildNumber by one in the file. Rest of the content should remain as is.



What I have come up with so far is bellow.



jq -e '.buildNumber + 1' 'package.json' > 'package.json.tmp' && cp 'package.json.tmp' 'package.json'


But after running the script I'm only left with the updated value in the package.json file, which is just 84. The rest of the content is gone. How do I fix this?










share|improve this question





























    1















    I have a simple JSON file like this,



    {

    ...
    "version": "2.1.0",
    "buildNumber": 83

    }


    I want to simply increment the buildNumber by one in the file. Rest of the content should remain as is.



    What I have come up with so far is bellow.



    jq -e '.buildNumber + 1' 'package.json' > 'package.json.tmp' && cp 'package.json.tmp' 'package.json'


    But after running the script I'm only left with the updated value in the package.json file, which is just 84. The rest of the content is gone. How do I fix this?










    share|improve this question



























      1












      1








      1








      I have a simple JSON file like this,



      {

      ...
      "version": "2.1.0",
      "buildNumber": 83

      }


      I want to simply increment the buildNumber by one in the file. Rest of the content should remain as is.



      What I have come up with so far is bellow.



      jq -e '.buildNumber + 1' 'package.json' > 'package.json.tmp' && cp 'package.json.tmp' 'package.json'


      But after running the script I'm only left with the updated value in the package.json file, which is just 84. The rest of the content is gone. How do I fix this?










      share|improve this question
















      I have a simple JSON file like this,



      {

      ...
      "version": "2.1.0",
      "buildNumber": 83

      }


      I want to simply increment the buildNumber by one in the file. Rest of the content should remain as is.



      What I have come up with so far is bellow.



      jq -e '.buildNumber + 1' 'package.json' > 'package.json.tmp' && cp 'package.json.tmp' 'package.json'


      But after running the script I'm only left with the updated value in the package.json file, which is just 84. The rest of the content is gone. How do I fix this?







      json increment jq






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 4:13









      peak

      33.2k94160




      33.2k94160










      asked Nov 21 '18 at 7:07









      JanithaRJanithaR

      6741918




      6741918
























          2 Answers
          2






          active

          oldest

          votes


















          0














          First, the filter to use is:



          .buildNumber += 1


          Second, as you evidently realize, it would be unwise to use > to overwrite the file. One option to consider if it’s admissible would be to use coreutil’s sponge. Another would be to rename the input file first.






          share|improve this answer
























          • I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doing jq '.buildNumber + 1' 'package.json' the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?

            – JanithaR
            Nov 21 '18 at 7:24











          • uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.

            – peak
            Nov 21 '18 at 7:28













          • So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I use curl and read the full content of the file and then do jq '.buildNumber += 1' 'package.json' and then carry on as is, that would give me the ouput I'm expecting yes?

            – JanithaR
            Nov 21 '18 at 7:38





















          -1














          I just put the two commands as bellow in two lines and it worked just as I wanted.



          jq '.buildNumber += 1' 'package.json' > 'package.json.tmp'
          cp 'package.json.tmp' 'package.json'





          share|improve this answer
























          • A safer variation would be along the lines of: mv package.json package.json.bak && jq ... < package.json.bak > package.json; that way, the original file contents should not be lost under any circumstances.

            – peak
            Nov 27 '18 at 4:18











          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%2f53406879%2fincrement-value-and-save-back-to-the-file%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









          0














          First, the filter to use is:



          .buildNumber += 1


          Second, as you evidently realize, it would be unwise to use > to overwrite the file. One option to consider if it’s admissible would be to use coreutil’s sponge. Another would be to rename the input file first.






          share|improve this answer
























          • I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doing jq '.buildNumber + 1' 'package.json' the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?

            – JanithaR
            Nov 21 '18 at 7:24











          • uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.

            – peak
            Nov 21 '18 at 7:28













          • So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I use curl and read the full content of the file and then do jq '.buildNumber += 1' 'package.json' and then carry on as is, that would give me the ouput I'm expecting yes?

            – JanithaR
            Nov 21 '18 at 7:38


















          0














          First, the filter to use is:



          .buildNumber += 1


          Second, as you evidently realize, it would be unwise to use > to overwrite the file. One option to consider if it’s admissible would be to use coreutil’s sponge. Another would be to rename the input file first.






          share|improve this answer
























          • I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doing jq '.buildNumber + 1' 'package.json' the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?

            – JanithaR
            Nov 21 '18 at 7:24











          • uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.

            – peak
            Nov 21 '18 at 7:28













          • So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I use curl and read the full content of the file and then do jq '.buildNumber += 1' 'package.json' and then carry on as is, that would give me the ouput I'm expecting yes?

            – JanithaR
            Nov 21 '18 at 7:38
















          0












          0








          0







          First, the filter to use is:



          .buildNumber += 1


          Second, as you evidently realize, it would be unwise to use > to overwrite the file. One option to consider if it’s admissible would be to use coreutil’s sponge. Another would be to rename the input file first.






          share|improve this answer













          First, the filter to use is:



          .buildNumber += 1


          Second, as you evidently realize, it would be unwise to use > to overwrite the file. One option to consider if it’s admissible would be to use coreutil’s sponge. Another would be to rename the input file first.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 7:14









          peakpeak

          33.2k94160




          33.2k94160













          • I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doing jq '.buildNumber + 1' 'package.json' the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?

            – JanithaR
            Nov 21 '18 at 7:24











          • uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.

            – peak
            Nov 21 '18 at 7:28













          • So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I use curl and read the full content of the file and then do jq '.buildNumber += 1' 'package.json' and then carry on as is, that would give me the ouput I'm expecting yes?

            – JanithaR
            Nov 21 '18 at 7:38





















          • I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doing jq '.buildNumber + 1' 'package.json' the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?

            – JanithaR
            Nov 21 '18 at 7:24











          • uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.

            – peak
            Nov 21 '18 at 7:28













          • So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I use curl and read the full content of the file and then do jq '.buildNumber += 1' 'package.json' and then carry on as is, that would give me the ouput I'm expecting yes?

            – JanithaR
            Nov 21 '18 at 7:38



















          I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doing jq '.buildNumber + 1' 'package.json' the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?

          – JanithaR
          Nov 21 '18 at 7:24





          I just found out about jq a couple of hours ago and I'm not quite familiar with bash every since I learned the basics of it like 8 years ago. I was under the impression that by doing jq '.buildNumber + 1' 'package.json' the full contents of the file would be in memory including the modification. And so I was simply writing that content to a temp file and the evidently replacing the original file with the temp file contents. Am I understanding how jq works wrong?

          – JanithaR
          Nov 21 '18 at 7:24













          uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.

          – peak
          Nov 21 '18 at 7:28







          uYes, that is incorrect. As you can easily check, .buildnumber + 1 adds 1 to .buildnumber, yielding an integer.

          – peak
          Nov 21 '18 at 7:28















          So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I use curl and read the full content of the file and then do jq '.buildNumber += 1' 'package.json' and then carry on as is, that would give me the ouput I'm expecting yes?

          – JanithaR
          Nov 21 '18 at 7:38







          So what I'm passing on to the package.json.temp file is simply that value, which in this case just 84. So if I use curl and read the full content of the file and then do jq '.buildNumber += 1' 'package.json' and then carry on as is, that would give me the ouput I'm expecting yes?

          – JanithaR
          Nov 21 '18 at 7:38















          -1














          I just put the two commands as bellow in two lines and it worked just as I wanted.



          jq '.buildNumber += 1' 'package.json' > 'package.json.tmp'
          cp 'package.json.tmp' 'package.json'





          share|improve this answer
























          • A safer variation would be along the lines of: mv package.json package.json.bak && jq ... < package.json.bak > package.json; that way, the original file contents should not be lost under any circumstances.

            – peak
            Nov 27 '18 at 4:18
















          -1














          I just put the two commands as bellow in two lines and it worked just as I wanted.



          jq '.buildNumber += 1' 'package.json' > 'package.json.tmp'
          cp 'package.json.tmp' 'package.json'





          share|improve this answer
























          • A safer variation would be along the lines of: mv package.json package.json.bak && jq ... < package.json.bak > package.json; that way, the original file contents should not be lost under any circumstances.

            – peak
            Nov 27 '18 at 4:18














          -1












          -1








          -1







          I just put the two commands as bellow in two lines and it worked just as I wanted.



          jq '.buildNumber += 1' 'package.json' > 'package.json.tmp'
          cp 'package.json.tmp' 'package.json'





          share|improve this answer













          I just put the two commands as bellow in two lines and it worked just as I wanted.



          jq '.buildNumber += 1' 'package.json' > 'package.json.tmp'
          cp 'package.json.tmp' 'package.json'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 14:57









          JanithaRJanithaR

          6741918




          6741918













          • A safer variation would be along the lines of: mv package.json package.json.bak && jq ... < package.json.bak > package.json; that way, the original file contents should not be lost under any circumstances.

            – peak
            Nov 27 '18 at 4:18



















          • A safer variation would be along the lines of: mv package.json package.json.bak && jq ... < package.json.bak > package.json; that way, the original file contents should not be lost under any circumstances.

            – peak
            Nov 27 '18 at 4:18

















          A safer variation would be along the lines of: mv package.json package.json.bak && jq ... < package.json.bak > package.json; that way, the original file contents should not be lost under any circumstances.

          – peak
          Nov 27 '18 at 4:18





          A safer variation would be along the lines of: mv package.json package.json.bak && jq ... < package.json.bak > package.json; that way, the original file contents should not be lost under any circumstances.

          – peak
          Nov 27 '18 at 4:18


















          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%2f53406879%2fincrement-value-and-save-back-to-the-file%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







          這個網誌中的熱門文章

          Hercules Kyvelos

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud