How to get data from JSON file with batch script and write the data into a text file?












0















I would like to get data from a *.json file into file.txt with batch.



My JSON file config.json:



  "nodes": [
{
"id": "item1",
"host": "${host:item1}",
"apps": [
{
"id": "value1",
"template_id": "value1"
},
{
"id": "value2",
"template_id": "value2",
},


I want to get only the values value1 and value2 of id elements of node apps. But the problem on using the command find in my script is actually that it reads the values of id and template_id.



Is it possible to get the value to id and not template_id?



I tried this... still not working...



setlocal EnableDelayedExpansion
set c=0
for /f "tokens=2 delims=:, " %%a in (' find "id" ^< "config.json" ') do (
set /a c+=1
set val[!c!]=%%~a
)
for /L %%b in (1,1,!c!) do echo !val[%%b]!


And after that, I don't know really how to get all these data in my text file.










share|improve this question





























    0















    I would like to get data from a *.json file into file.txt with batch.



    My JSON file config.json:



      "nodes": [
    {
    "id": "item1",
    "host": "${host:item1}",
    "apps": [
    {
    "id": "value1",
    "template_id": "value1"
    },
    {
    "id": "value2",
    "template_id": "value2",
    },


    I want to get only the values value1 and value2 of id elements of node apps. But the problem on using the command find in my script is actually that it reads the values of id and template_id.



    Is it possible to get the value to id and not template_id?



    I tried this... still not working...



    setlocal EnableDelayedExpansion
    set c=0
    for /f "tokens=2 delims=:, " %%a in (' find "id" ^< "config.json" ') do (
    set /a c+=1
    set val[!c!]=%%~a
    )
    for /L %%b in (1,1,!c!) do echo !val[%%b]!


    And after that, I don't know really how to get all these data in my text file.










    share|improve this question



























      0












      0








      0








      I would like to get data from a *.json file into file.txt with batch.



      My JSON file config.json:



        "nodes": [
      {
      "id": "item1",
      "host": "${host:item1}",
      "apps": [
      {
      "id": "value1",
      "template_id": "value1"
      },
      {
      "id": "value2",
      "template_id": "value2",
      },


      I want to get only the values value1 and value2 of id elements of node apps. But the problem on using the command find in my script is actually that it reads the values of id and template_id.



      Is it possible to get the value to id and not template_id?



      I tried this... still not working...



      setlocal EnableDelayedExpansion
      set c=0
      for /f "tokens=2 delims=:, " %%a in (' find "id" ^< "config.json" ') do (
      set /a c+=1
      set val[!c!]=%%~a
      )
      for /L %%b in (1,1,!c!) do echo !val[%%b]!


      And after that, I don't know really how to get all these data in my text file.










      share|improve this question
















      I would like to get data from a *.json file into file.txt with batch.



      My JSON file config.json:



        "nodes": [
      {
      "id": "item1",
      "host": "${host:item1}",
      "apps": [
      {
      "id": "value1",
      "template_id": "value1"
      },
      {
      "id": "value2",
      "template_id": "value2",
      },


      I want to get only the values value1 and value2 of id elements of node apps. But the problem on using the command find in my script is actually that it reads the values of id and template_id.



      Is it possible to get the value to id and not template_id?



      I tried this... still not working...



      setlocal EnableDelayedExpansion
      set c=0
      for /f "tokens=2 delims=:, " %%a in (' find "id" ^< "config.json" ') do (
      set /a c+=1
      set val[!c!]=%%~a
      )
      for /L %%b in (1,1,!c!) do echo !val[%%b]!


      And after that, I don't know really how to get all these data in my text file.







      json batch-file






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 19:36









      Mofi

      28k83777




      28k83777










      asked Nov 24 '18 at 17:43









      programmation1994programmation1994

      158




      158
























          3 Answers
          3






          active

          oldest

          votes


















          0














          You shouldn't treat structured information as plain text.



          If I take for example a Valid JSON (RFC 4627) version of your fragment:



          {
          "nodes":[
          {
          "id":"item1",
          "host":"${host:item1}",
          "apps":[
          {
          "id":"value1",
          "template_id":"value1"
          },
          {
          "id":"value2",
          "template_id":"value2"
          }
          ]
          }]
          }


          And use PowerShell's cmdlet ConvertFrom-Json it's quite easy to receive the proper ID values



          PoSh> $Json = Get-Content .config.json | ConvertFrom-Json
          PoSh> $JSon.Nodes.Apps.id
          value1
          value2


          To be on topic with the batch-file tag wrapping this in a batch



          @Echo off
          Powershell -Nop -C "(Get-Content .config.json|ConvertFrom-Json).Nodes.Apps.id" >file.txt





          share|improve this answer
























          • Thanks you very much ! it works very well !

            – programmation1994
            Nov 24 '18 at 19:12











          • I have just another question, if i want to get in my .txt this values : item1 then value 1 and value 2. how can i do that with powershell ?

            – programmation1994
            Nov 24 '18 at 19:18













          • You' need to concatenate the information from different levels. Powershell -Nop -C "(Get-Content .config.json|ConvertFrom-Json).Nodes.id" >file.txt and append the the other command to the file with >>file.txt

            – LotPings
            Nov 24 '18 at 19:20













          • Thanks for your answer ! Actuallys this working but if i have in my .json "item2" "value3" and "value4" if will show me : item1 - item2 - value1-value2-value3-value4 but it doesn't see me item1-value1-value2-item2-value3-value4 ?

            – programmation1994
            Nov 24 '18 at 19:42











          • Would have been nice to get this requirement from the beginning, in fact this is a new question. As you see by yourself wrapping only halfway ready solutions in another script language get's annoying. In the above powershell code the whole structure is contained in the variable $Json and you can access them directly with a (zero based) index. $json.nodes[1].id, $json.nodes[1].apps.id

            – LotPings
            Nov 24 '18 at 19:55



















          0














          You can try with jsonextractor.bat . In order to have a valid json I've used this:



          {
          "nodes": [{
          "id": "item1",
          "host": "${host:item1}",
          "apps": [{
          "id": "value1",
          "template_id": "value1"
          },
          {
          "id": "value2",
          "template_id": "value2"
          }
          ]
          }]
          }


          and to get the values:



          for /f "tokens=* delims=" %%a in ('jsonextractor.bat test.json "nodes[0].apps[0].id"') do (
          set "first_id=%%~a"
          )
          echo %first_id%





          share|improve this answer

































            0














            Please use a decent JSON parser, like Xidel, to parse JSON!



            xidel -s config.json -e "$json/(.//apps)()/id"   # or in full: $json/(nodes)()/(apps)()/id
            value1
            value2


            To save the output to a file:



            xidel -s config.json -e "$json/(.//apps)()/id" > output.txt





            share|improve this answer























              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%2f53460823%2fhow-to-get-data-from-json-file-with-batch-script-and-write-the-data-into-a-text%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You shouldn't treat structured information as plain text.



              If I take for example a Valid JSON (RFC 4627) version of your fragment:



              {
              "nodes":[
              {
              "id":"item1",
              "host":"${host:item1}",
              "apps":[
              {
              "id":"value1",
              "template_id":"value1"
              },
              {
              "id":"value2",
              "template_id":"value2"
              }
              ]
              }]
              }


              And use PowerShell's cmdlet ConvertFrom-Json it's quite easy to receive the proper ID values



              PoSh> $Json = Get-Content .config.json | ConvertFrom-Json
              PoSh> $JSon.Nodes.Apps.id
              value1
              value2


              To be on topic with the batch-file tag wrapping this in a batch



              @Echo off
              Powershell -Nop -C "(Get-Content .config.json|ConvertFrom-Json).Nodes.Apps.id" >file.txt





              share|improve this answer
























              • Thanks you very much ! it works very well !

                – programmation1994
                Nov 24 '18 at 19:12











              • I have just another question, if i want to get in my .txt this values : item1 then value 1 and value 2. how can i do that with powershell ?

                – programmation1994
                Nov 24 '18 at 19:18













              • You' need to concatenate the information from different levels. Powershell -Nop -C "(Get-Content .config.json|ConvertFrom-Json).Nodes.id" >file.txt and append the the other command to the file with >>file.txt

                – LotPings
                Nov 24 '18 at 19:20













              • Thanks for your answer ! Actuallys this working but if i have in my .json "item2" "value3" and "value4" if will show me : item1 - item2 - value1-value2-value3-value4 but it doesn't see me item1-value1-value2-item2-value3-value4 ?

                – programmation1994
                Nov 24 '18 at 19:42











              • Would have been nice to get this requirement from the beginning, in fact this is a new question. As you see by yourself wrapping only halfway ready solutions in another script language get's annoying. In the above powershell code the whole structure is contained in the variable $Json and you can access them directly with a (zero based) index. $json.nodes[1].id, $json.nodes[1].apps.id

                – LotPings
                Nov 24 '18 at 19:55
















              0














              You shouldn't treat structured information as plain text.



              If I take for example a Valid JSON (RFC 4627) version of your fragment:



              {
              "nodes":[
              {
              "id":"item1",
              "host":"${host:item1}",
              "apps":[
              {
              "id":"value1",
              "template_id":"value1"
              },
              {
              "id":"value2",
              "template_id":"value2"
              }
              ]
              }]
              }


              And use PowerShell's cmdlet ConvertFrom-Json it's quite easy to receive the proper ID values



              PoSh> $Json = Get-Content .config.json | ConvertFrom-Json
              PoSh> $JSon.Nodes.Apps.id
              value1
              value2


              To be on topic with the batch-file tag wrapping this in a batch



              @Echo off
              Powershell -Nop -C "(Get-Content .config.json|ConvertFrom-Json).Nodes.Apps.id" >file.txt





              share|improve this answer
























              • Thanks you very much ! it works very well !

                – programmation1994
                Nov 24 '18 at 19:12











              • I have just another question, if i want to get in my .txt this values : item1 then value 1 and value 2. how can i do that with powershell ?

                – programmation1994
                Nov 24 '18 at 19:18













              • You' need to concatenate the information from different levels. Powershell -Nop -C "(Get-Content .config.json|ConvertFrom-Json).Nodes.id" >file.txt and append the the other command to the file with >>file.txt

                – LotPings
                Nov 24 '18 at 19:20













              • Thanks for your answer ! Actuallys this working but if i have in my .json "item2" "value3" and "value4" if will show me : item1 - item2 - value1-value2-value3-value4 but it doesn't see me item1-value1-value2-item2-value3-value4 ?

                – programmation1994
                Nov 24 '18 at 19:42











              • Would have been nice to get this requirement from the beginning, in fact this is a new question. As you see by yourself wrapping only halfway ready solutions in another script language get's annoying. In the above powershell code the whole structure is contained in the variable $Json and you can access them directly with a (zero based) index. $json.nodes[1].id, $json.nodes[1].apps.id

                – LotPings
                Nov 24 '18 at 19:55














              0












              0








              0







              You shouldn't treat structured information as plain text.



              If I take for example a Valid JSON (RFC 4627) version of your fragment:



              {
              "nodes":[
              {
              "id":"item1",
              "host":"${host:item1}",
              "apps":[
              {
              "id":"value1",
              "template_id":"value1"
              },
              {
              "id":"value2",
              "template_id":"value2"
              }
              ]
              }]
              }


              And use PowerShell's cmdlet ConvertFrom-Json it's quite easy to receive the proper ID values



              PoSh> $Json = Get-Content .config.json | ConvertFrom-Json
              PoSh> $JSon.Nodes.Apps.id
              value1
              value2


              To be on topic with the batch-file tag wrapping this in a batch



              @Echo off
              Powershell -Nop -C "(Get-Content .config.json|ConvertFrom-Json).Nodes.Apps.id" >file.txt





              share|improve this answer













              You shouldn't treat structured information as plain text.



              If I take for example a Valid JSON (RFC 4627) version of your fragment:



              {
              "nodes":[
              {
              "id":"item1",
              "host":"${host:item1}",
              "apps":[
              {
              "id":"value1",
              "template_id":"value1"
              },
              {
              "id":"value2",
              "template_id":"value2"
              }
              ]
              }]
              }


              And use PowerShell's cmdlet ConvertFrom-Json it's quite easy to receive the proper ID values



              PoSh> $Json = Get-Content .config.json | ConvertFrom-Json
              PoSh> $JSon.Nodes.Apps.id
              value1
              value2


              To be on topic with the batch-file tag wrapping this in a batch



              @Echo off
              Powershell -Nop -C "(Get-Content .config.json|ConvertFrom-Json).Nodes.Apps.id" >file.txt






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 24 '18 at 18:44









              LotPingsLotPings

              18.4k61532




              18.4k61532













              • Thanks you very much ! it works very well !

                – programmation1994
                Nov 24 '18 at 19:12











              • I have just another question, if i want to get in my .txt this values : item1 then value 1 and value 2. how can i do that with powershell ?

                – programmation1994
                Nov 24 '18 at 19:18













              • You' need to concatenate the information from different levels. Powershell -Nop -C "(Get-Content .config.json|ConvertFrom-Json).Nodes.id" >file.txt and append the the other command to the file with >>file.txt

                – LotPings
                Nov 24 '18 at 19:20













              • Thanks for your answer ! Actuallys this working but if i have in my .json "item2" "value3" and "value4" if will show me : item1 - item2 - value1-value2-value3-value4 but it doesn't see me item1-value1-value2-item2-value3-value4 ?

                – programmation1994
                Nov 24 '18 at 19:42











              • Would have been nice to get this requirement from the beginning, in fact this is a new question. As you see by yourself wrapping only halfway ready solutions in another script language get's annoying. In the above powershell code the whole structure is contained in the variable $Json and you can access them directly with a (zero based) index. $json.nodes[1].id, $json.nodes[1].apps.id

                – LotPings
                Nov 24 '18 at 19:55



















              • Thanks you very much ! it works very well !

                – programmation1994
                Nov 24 '18 at 19:12











              • I have just another question, if i want to get in my .txt this values : item1 then value 1 and value 2. how can i do that with powershell ?

                – programmation1994
                Nov 24 '18 at 19:18













              • You' need to concatenate the information from different levels. Powershell -Nop -C "(Get-Content .config.json|ConvertFrom-Json).Nodes.id" >file.txt and append the the other command to the file with >>file.txt

                – LotPings
                Nov 24 '18 at 19:20













              • Thanks for your answer ! Actuallys this working but if i have in my .json "item2" "value3" and "value4" if will show me : item1 - item2 - value1-value2-value3-value4 but it doesn't see me item1-value1-value2-item2-value3-value4 ?

                – programmation1994
                Nov 24 '18 at 19:42











              • Would have been nice to get this requirement from the beginning, in fact this is a new question. As you see by yourself wrapping only halfway ready solutions in another script language get's annoying. In the above powershell code the whole structure is contained in the variable $Json and you can access them directly with a (zero based) index. $json.nodes[1].id, $json.nodes[1].apps.id

                – LotPings
                Nov 24 '18 at 19:55

















              Thanks you very much ! it works very well !

              – programmation1994
              Nov 24 '18 at 19:12





              Thanks you very much ! it works very well !

              – programmation1994
              Nov 24 '18 at 19:12













              I have just another question, if i want to get in my .txt this values : item1 then value 1 and value 2. how can i do that with powershell ?

              – programmation1994
              Nov 24 '18 at 19:18







              I have just another question, if i want to get in my .txt this values : item1 then value 1 and value 2. how can i do that with powershell ?

              – programmation1994
              Nov 24 '18 at 19:18















              You' need to concatenate the information from different levels. Powershell -Nop -C "(Get-Content .config.json|ConvertFrom-Json).Nodes.id" >file.txt and append the the other command to the file with >>file.txt

              – LotPings
              Nov 24 '18 at 19:20







              You' need to concatenate the information from different levels. Powershell -Nop -C "(Get-Content .config.json|ConvertFrom-Json).Nodes.id" >file.txt and append the the other command to the file with >>file.txt

              – LotPings
              Nov 24 '18 at 19:20















              Thanks for your answer ! Actuallys this working but if i have in my .json "item2" "value3" and "value4" if will show me : item1 - item2 - value1-value2-value3-value4 but it doesn't see me item1-value1-value2-item2-value3-value4 ?

              – programmation1994
              Nov 24 '18 at 19:42





              Thanks for your answer ! Actuallys this working but if i have in my .json "item2" "value3" and "value4" if will show me : item1 - item2 - value1-value2-value3-value4 but it doesn't see me item1-value1-value2-item2-value3-value4 ?

              – programmation1994
              Nov 24 '18 at 19:42













              Would have been nice to get this requirement from the beginning, in fact this is a new question. As you see by yourself wrapping only halfway ready solutions in another script language get's annoying. In the above powershell code the whole structure is contained in the variable $Json and you can access them directly with a (zero based) index. $json.nodes[1].id, $json.nodes[1].apps.id

              – LotPings
              Nov 24 '18 at 19:55





              Would have been nice to get this requirement from the beginning, in fact this is a new question. As you see by yourself wrapping only halfway ready solutions in another script language get's annoying. In the above powershell code the whole structure is contained in the variable $Json and you can access them directly with a (zero based) index. $json.nodes[1].id, $json.nodes[1].apps.id

              – LotPings
              Nov 24 '18 at 19:55













              0














              You can try with jsonextractor.bat . In order to have a valid json I've used this:



              {
              "nodes": [{
              "id": "item1",
              "host": "${host:item1}",
              "apps": [{
              "id": "value1",
              "template_id": "value1"
              },
              {
              "id": "value2",
              "template_id": "value2"
              }
              ]
              }]
              }


              and to get the values:



              for /f "tokens=* delims=" %%a in ('jsonextractor.bat test.json "nodes[0].apps[0].id"') do (
              set "first_id=%%~a"
              )
              echo %first_id%





              share|improve this answer






























                0














                You can try with jsonextractor.bat . In order to have a valid json I've used this:



                {
                "nodes": [{
                "id": "item1",
                "host": "${host:item1}",
                "apps": [{
                "id": "value1",
                "template_id": "value1"
                },
                {
                "id": "value2",
                "template_id": "value2"
                }
                ]
                }]
                }


                and to get the values:



                for /f "tokens=* delims=" %%a in ('jsonextractor.bat test.json "nodes[0].apps[0].id"') do (
                set "first_id=%%~a"
                )
                echo %first_id%





                share|improve this answer




























                  0












                  0








                  0







                  You can try with jsonextractor.bat . In order to have a valid json I've used this:



                  {
                  "nodes": [{
                  "id": "item1",
                  "host": "${host:item1}",
                  "apps": [{
                  "id": "value1",
                  "template_id": "value1"
                  },
                  {
                  "id": "value2",
                  "template_id": "value2"
                  }
                  ]
                  }]
                  }


                  and to get the values:



                  for /f "tokens=* delims=" %%a in ('jsonextractor.bat test.json "nodes[0].apps[0].id"') do (
                  set "first_id=%%~a"
                  )
                  echo %first_id%





                  share|improve this answer















                  You can try with jsonextractor.bat . In order to have a valid json I've used this:



                  {
                  "nodes": [{
                  "id": "item1",
                  "host": "${host:item1}",
                  "apps": [{
                  "id": "value1",
                  "template_id": "value1"
                  },
                  {
                  "id": "value2",
                  "template_id": "value2"
                  }
                  ]
                  }]
                  }


                  and to get the values:



                  for /f "tokens=* delims=" %%a in ('jsonextractor.bat test.json "nodes[0].apps[0].id"') do (
                  set "first_id=%%~a"
                  )
                  echo %first_id%






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 24 '18 at 19:44









                  Mofi

                  28k83777




                  28k83777










                  answered Nov 24 '18 at 18:48









                  npocmakanpocmaka

                  41.7k1185128




                  41.7k1185128























                      0














                      Please use a decent JSON parser, like Xidel, to parse JSON!



                      xidel -s config.json -e "$json/(.//apps)()/id"   # or in full: $json/(nodes)()/(apps)()/id
                      value1
                      value2


                      To save the output to a file:



                      xidel -s config.json -e "$json/(.//apps)()/id" > output.txt





                      share|improve this answer




























                        0














                        Please use a decent JSON parser, like Xidel, to parse JSON!



                        xidel -s config.json -e "$json/(.//apps)()/id"   # or in full: $json/(nodes)()/(apps)()/id
                        value1
                        value2


                        To save the output to a file:



                        xidel -s config.json -e "$json/(.//apps)()/id" > output.txt





                        share|improve this answer


























                          0












                          0








                          0







                          Please use a decent JSON parser, like Xidel, to parse JSON!



                          xidel -s config.json -e "$json/(.//apps)()/id"   # or in full: $json/(nodes)()/(apps)()/id
                          value1
                          value2


                          To save the output to a file:



                          xidel -s config.json -e "$json/(.//apps)()/id" > output.txt





                          share|improve this answer













                          Please use a decent JSON parser, like Xidel, to parse JSON!



                          xidel -s config.json -e "$json/(.//apps)()/id"   # or in full: $json/(nodes)()/(apps)()/id
                          value1
                          value2


                          To save the output to a file:



                          xidel -s config.json -e "$json/(.//apps)()/id" > output.txt






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 7 at 17:51









                          ReinoReino

                          1548




                          1548






























                              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%2f53460823%2fhow-to-get-data-from-json-file-with-batch-script-and-write-the-data-into-a-text%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

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                              Calculate evaluation metrics using cross_val_predict sklearn

                              Insert data from modal to MySQL (multiple modal on website)