handling null (empty array) while converting json to csv with jq?openweathermap json to csvConvert JSON to...

Source of story about the Vilna Gaon and immigration policy

Can a Resident Assistant Be Told to Ignore a Lawful Order?

When does Fisher's "go get more data" approach make sense?

Why hasn't the U.S. government paid war reparations to any country it attacked?

Did 007 exist before James Bond?

Why is "dark" an adverb in this sentence?

What's the phrasal verb for carbonated drinks exploding out of the can after being shaken?

Re-negotiate salary once I earn my diploma

Sending a photo of my bank account card to the future employer

What alternatives exist to at-will employment?

What exactly is a Hadouken?

Is this more than a packing puzzle?

Could I use a greatsword and a longsword in one turn with Two-Weapon Fighting and the Dual Wielder feat?

Will it hurt my career to work as a graphic designer in a startup for beauty and skin care?

Why does FFmpeg choose 10+20+20 ms instead of an even 16 ms for 60 fps GIF images?

Why run a service as a system user?

Why should I cook the flour first when making bechamel sauce?

Can both line and load go to same screw on a GFCI outlet?

Cauchy reals and Dedekind reals satisfy "the same mathematical theorems"

How could an animal "smell" carbon monoxide?

What is the superlative of ipse?

Is there a good program to play chess online in ubuntu?

What are "full piece" and "half piece" in chess?

What is this old "lemon-squeezer" shaped pan



handling null (empty array) while converting json to csv with jq?


openweathermap json to csvConvert JSON to CSVIdentify .json files with empty arrays?Export JSON to CSV with Headers using JQSplit a json array to multiple fileslooping through JSON array in shell scriptHow to get value with JQ (JSON)JSON -> csv creating header line and padding header if found empty fieldLoop into JSON Array in Bash ScriptCSV to NESTED JSON






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















When trying to convert json to csv, I get an error about null with the following jq query:



printf "[]" | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'


It gives the following error,



jq: error (at <stdin>:0): Cannot iterate over null (null)


but works fine if the json array is non-empty and has an object in it:



$ printf '[{"a":1}]' | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'
"a"
1


The jq grammar looks great, but I am not familiar with it as I just started to use this tool.



Can someone please explain how to patch the query to output nothing when the array is empty? (first example).










share|improve this question





























    0















    When trying to convert json to csv, I get an error about null with the following jq query:



    printf "[]" | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'


    It gives the following error,



    jq: error (at <stdin>:0): Cannot iterate over null (null)


    but works fine if the json array is non-empty and has an object in it:



    $ printf '[{"a":1}]' | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'
    "a"
    1


    The jq grammar looks great, but I am not familiar with it as I just started to use this tool.



    Can someone please explain how to patch the query to output nothing when the array is empty? (first example).










    share|improve this question

























      0












      0








      0








      When trying to convert json to csv, I get an error about null with the following jq query:



      printf "[]" | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'


      It gives the following error,



      jq: error (at <stdin>:0): Cannot iterate over null (null)


      but works fine if the json array is non-empty and has an object in it:



      $ printf '[{"a":1}]' | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'
      "a"
      1


      The jq grammar looks great, but I am not familiar with it as I just started to use this tool.



      Can someone please explain how to patch the query to output nothing when the array is empty? (first example).










      share|improve this question














      When trying to convert json to csv, I get an error about null with the following jq query:



      printf "[]" | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'


      It gives the following error,



      jq: error (at <stdin>:0): Cannot iterate over null (null)


      but works fine if the json array is non-empty and has an object in it:



      $ printf '[{"a":1}]' | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'
      "a"
      1


      The jq grammar looks great, but I am not familiar with it as I just started to use this tool.



      Can someone please explain how to patch the query to output nothing when the array is empty? (first example).







      ubuntu csv json jq






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      tinlyxtinlyx

      1401 gold badge1 silver badge7 bronze badges




      1401 gold badge1 silver badge7 bronze badges






















          0






          active

          oldest

          votes














          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2funix.stackexchange.com%2fquestions%2f530349%2fhandling-null-empty-array-while-converting-json-to-csv-with-jq%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • 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%2funix.stackexchange.com%2fquestions%2f530349%2fhandling-null-empty-array-while-converting-json-to-csv-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

          Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

          The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

          Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...