Turning list of headers and list of values into valid json with jq












0















I'm working with an endpoint like this:



curl -S www.example.com 
[["A", "B", "C", "D"]n
["1","2",null,"4"]]


I'd like to use jq to change this response into proper json rather than a list of headers and a row of data corresponding to the items in the list:



{"A": "1", "B": "2", "C" : null, "D": "4"}


However, what seems like the obvious answer from here



of



curl -S www.example.com  | jq '{(.[0]) : .[1] }'


produces



jq: error (at <stdin>:1): Cannot use array (["A...) as object key


And



curl -S www.example.com  | jq '{.[0] : .[1] }'


produces



jq: error: syntax error, unexpected '.' (Unix shell quoting issues?) at <top-level>, line 1:
{.[0] : .[1] }
jq: 1 compile error


What's the right syntax for



[[HEADER1,HEADER2,...]n
[DATA1,DATA2]]


responses into json?



Are the unquoted null and the newline the source of my problem?










share|improve this question





























    0















    I'm working with an endpoint like this:



    curl -S www.example.com 
    [["A", "B", "C", "D"]n
    ["1","2",null,"4"]]


    I'd like to use jq to change this response into proper json rather than a list of headers and a row of data corresponding to the items in the list:



    {"A": "1", "B": "2", "C" : null, "D": "4"}


    However, what seems like the obvious answer from here



    of



    curl -S www.example.com  | jq '{(.[0]) : .[1] }'


    produces



    jq: error (at <stdin>:1): Cannot use array (["A...) as object key


    And



    curl -S www.example.com  | jq '{.[0] : .[1] }'


    produces



    jq: error: syntax error, unexpected '.' (Unix shell quoting issues?) at <top-level>, line 1:
    {.[0] : .[1] }
    jq: 1 compile error


    What's the right syntax for



    [[HEADER1,HEADER2,...]n
    [DATA1,DATA2]]


    responses into json?



    Are the unquoted null and the newline the source of my problem?










    share|improve this question



























      0












      0








      0








      I'm working with an endpoint like this:



      curl -S www.example.com 
      [["A", "B", "C", "D"]n
      ["1","2",null,"4"]]


      I'd like to use jq to change this response into proper json rather than a list of headers and a row of data corresponding to the items in the list:



      {"A": "1", "B": "2", "C" : null, "D": "4"}


      However, what seems like the obvious answer from here



      of



      curl -S www.example.com  | jq '{(.[0]) : .[1] }'


      produces



      jq: error (at <stdin>:1): Cannot use array (["A...) as object key


      And



      curl -S www.example.com  | jq '{.[0] : .[1] }'


      produces



      jq: error: syntax error, unexpected '.' (Unix shell quoting issues?) at <top-level>, line 1:
      {.[0] : .[1] }
      jq: 1 compile error


      What's the right syntax for



      [[HEADER1,HEADER2,...]n
      [DATA1,DATA2]]


      responses into json?



      Are the unquoted null and the newline the source of my problem?










      share|improve this question
















      I'm working with an endpoint like this:



      curl -S www.example.com 
      [["A", "B", "C", "D"]n
      ["1","2",null,"4"]]


      I'd like to use jq to change this response into proper json rather than a list of headers and a row of data corresponding to the items in the list:



      {"A": "1", "B": "2", "C" : null, "D": "4"}


      However, what seems like the obvious answer from here



      of



      curl -S www.example.com  | jq '{(.[0]) : .[1] }'


      produces



      jq: error (at <stdin>:1): Cannot use array (["A...) as object key


      And



      curl -S www.example.com  | jq '{.[0] : .[1] }'


      produces



      jq: error: syntax error, unexpected '.' (Unix shell quoting issues?) at <top-level>, line 1:
      {.[0] : .[1] }
      jq: 1 compile error


      What's the right syntax for



      [[HEADER1,HEADER2,...]n
      [DATA1,DATA2]]


      responses into json?



      Are the unquoted null and the newline the source of my problem?







      json jq






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 7:00









      peak

      30.8k83957




      30.8k83957










      asked Nov 19 '18 at 21:47









      MittenchopsMittenchops

      6,4042165137




      6,4042165137
























          1 Answer
          1






          active

          oldest

          votes


















          1














          According to what's posted, the output of the curl command is so weird that it would require special processing. Chances are the output is not as weird as shown, and the sed command shown as part of the solution below might need tweaking.



          In any case, with the input as shown:



          sed 's/\"/"/;1s/\n/,/' input.txt | jq '
          .[0] as $h
          | .[1]
          | . as $in
          | reduce range(0;length) as $i
          ({}; .[$h[$i]] = $in[$i])'


          produces the desired output.



          One-stop shopping



          jq -Rs < input.txt '
          split("n")
          | map(select(length>0))
          | ( .[0] | sub("[\\"; "") | sub("\\n"; "") | fromjson) as $h
          | .[1]
          | sub("\]\]"; "]")
          | fromjson
          | . as $in
          | reduce range(0;length) as $i
          ({}; .[$h[$i]] = $in[$i])'





          share|improve this answer


























          • Wow, you are very skilled at jq. Thank you!

            – Mittenchops
            Nov 19 '18 at 23:56











          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%2f53383105%2fturning-list-of-headers-and-list-of-values-into-valid-json-with-jq%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          According to what's posted, the output of the curl command is so weird that it would require special processing. Chances are the output is not as weird as shown, and the sed command shown as part of the solution below might need tweaking.



          In any case, with the input as shown:



          sed 's/\"/"/;1s/\n/,/' input.txt | jq '
          .[0] as $h
          | .[1]
          | . as $in
          | reduce range(0;length) as $i
          ({}; .[$h[$i]] = $in[$i])'


          produces the desired output.



          One-stop shopping



          jq -Rs < input.txt '
          split("n")
          | map(select(length>0))
          | ( .[0] | sub("[\\"; "") | sub("\\n"; "") | fromjson) as $h
          | .[1]
          | sub("\]\]"; "]")
          | fromjson
          | . as $in
          | reduce range(0;length) as $i
          ({}; .[$h[$i]] = $in[$i])'





          share|improve this answer


























          • Wow, you are very skilled at jq. Thank you!

            – Mittenchops
            Nov 19 '18 at 23:56
















          1














          According to what's posted, the output of the curl command is so weird that it would require special processing. Chances are the output is not as weird as shown, and the sed command shown as part of the solution below might need tweaking.



          In any case, with the input as shown:



          sed 's/\"/"/;1s/\n/,/' input.txt | jq '
          .[0] as $h
          | .[1]
          | . as $in
          | reduce range(0;length) as $i
          ({}; .[$h[$i]] = $in[$i])'


          produces the desired output.



          One-stop shopping



          jq -Rs < input.txt '
          split("n")
          | map(select(length>0))
          | ( .[0] | sub("[\\"; "") | sub("\\n"; "") | fromjson) as $h
          | .[1]
          | sub("\]\]"; "]")
          | fromjson
          | . as $in
          | reduce range(0;length) as $i
          ({}; .[$h[$i]] = $in[$i])'





          share|improve this answer


























          • Wow, you are very skilled at jq. Thank you!

            – Mittenchops
            Nov 19 '18 at 23:56














          1












          1








          1







          According to what's posted, the output of the curl command is so weird that it would require special processing. Chances are the output is not as weird as shown, and the sed command shown as part of the solution below might need tweaking.



          In any case, with the input as shown:



          sed 's/\"/"/;1s/\n/,/' input.txt | jq '
          .[0] as $h
          | .[1]
          | . as $in
          | reduce range(0;length) as $i
          ({}; .[$h[$i]] = $in[$i])'


          produces the desired output.



          One-stop shopping



          jq -Rs < input.txt '
          split("n")
          | map(select(length>0))
          | ( .[0] | sub("[\\"; "") | sub("\\n"; "") | fromjson) as $h
          | .[1]
          | sub("\]\]"; "]")
          | fromjson
          | . as $in
          | reduce range(0;length) as $i
          ({}; .[$h[$i]] = $in[$i])'





          share|improve this answer















          According to what's posted, the output of the curl command is so weird that it would require special processing. Chances are the output is not as weird as shown, and the sed command shown as part of the solution below might need tweaking.



          In any case, with the input as shown:



          sed 's/\"/"/;1s/\n/,/' input.txt | jq '
          .[0] as $h
          | .[1]
          | . as $in
          | reduce range(0;length) as $i
          ({}; .[$h[$i]] = $in[$i])'


          produces the desired output.



          One-stop shopping



          jq -Rs < input.txt '
          split("n")
          | map(select(length>0))
          | ( .[0] | sub("[\\"; "") | sub("\\n"; "") | fromjson) as $h
          | .[1]
          | sub("\]\]"; "]")
          | fromjson
          | . as $in
          | reduce range(0;length) as $i
          ({}; .[$h[$i]] = $in[$i])'






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 3:34

























          answered Nov 19 '18 at 22:22









          peakpeak

          30.8k83957




          30.8k83957













          • Wow, you are very skilled at jq. Thank you!

            – Mittenchops
            Nov 19 '18 at 23:56



















          • Wow, you are very skilled at jq. Thank you!

            – Mittenchops
            Nov 19 '18 at 23:56

















          Wow, you are very skilled at jq. Thank you!

          – Mittenchops
          Nov 19 '18 at 23:56





          Wow, you are very skilled at jq. Thank you!

          – Mittenchops
          Nov 19 '18 at 23:56


















          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%2f53383105%2fturning-list-of-headers-and-list-of-values-into-valid-json-with-jq%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

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith