parse JSON data using jq and convert into csv












0















I am downloading api json data and converting into my final result as a csv file.



The sample data is in the below form:



{
"content": [{
"Title": "abc",
"brand": "xyz",
"information": {
"c1": "101",
"c2": "11111",
"c3": "a,b,c,d,e:abc."
}
},
{
"Title": "RX100",
"brand": "Yamaha",
"information": {
"c1": "102",
"c2": "22222",
"c3": "a."
}
},
{
"Title": "victor",
"brand": "TVS",
"information": {
"c1": "103",
"c2": "33333",
"c3": "a,b,c"
}
},
{
"Title": "R15",
"brand": "Yamaha",
"information": {
"c1": "104",
"c2": "44444",
"c3": "a,b"
}
}
]
}


I have successfully downloaded and converted into multiple csv on the basis of number of Title.
Data looks like in the below form after converting in multiple csv file.



Headers-> c1,c2,c3
csv1--> 101,11111,a,b,c,d,e:abc.
csv2--> 102,22222,a.
csv3--> 103,33333,a,b,c.
csv4--> 104,44444,a,b.


but I want the above data in below form.



Headers-> c1,c2,c3,c4,c5,c6,c7
csv1--> 101,11111,a,b,c,d,e:abc.
csv2--> 102,22222,a.
csv3--> 103,33333,a,b,c.
csv4--> 104,44444,a,b.


Is it possible to divide the c3 into different number of columns on the basis of "," present in my json file using json. The column c3 will get divided on the basis of number of elements are present and will be the maximum value of c3 data.










share|improve this question



























    0















    I am downloading api json data and converting into my final result as a csv file.



    The sample data is in the below form:



    {
    "content": [{
    "Title": "abc",
    "brand": "xyz",
    "information": {
    "c1": "101",
    "c2": "11111",
    "c3": "a,b,c,d,e:abc."
    }
    },
    {
    "Title": "RX100",
    "brand": "Yamaha",
    "information": {
    "c1": "102",
    "c2": "22222",
    "c3": "a."
    }
    },
    {
    "Title": "victor",
    "brand": "TVS",
    "information": {
    "c1": "103",
    "c2": "33333",
    "c3": "a,b,c"
    }
    },
    {
    "Title": "R15",
    "brand": "Yamaha",
    "information": {
    "c1": "104",
    "c2": "44444",
    "c3": "a,b"
    }
    }
    ]
    }


    I have successfully downloaded and converted into multiple csv on the basis of number of Title.
    Data looks like in the below form after converting in multiple csv file.



    Headers-> c1,c2,c3
    csv1--> 101,11111,a,b,c,d,e:abc.
    csv2--> 102,22222,a.
    csv3--> 103,33333,a,b,c.
    csv4--> 104,44444,a,b.


    but I want the above data in below form.



    Headers-> c1,c2,c3,c4,c5,c6,c7
    csv1--> 101,11111,a,b,c,d,e:abc.
    csv2--> 102,22222,a.
    csv3--> 103,33333,a,b,c.
    csv4--> 104,44444,a,b.


    Is it possible to divide the c3 into different number of columns on the basis of "," present in my json file using json. The column c3 will get divided on the basis of number of elements are present and will be the maximum value of c3 data.










    share|improve this question

























      0












      0








      0








      I am downloading api json data and converting into my final result as a csv file.



      The sample data is in the below form:



      {
      "content": [{
      "Title": "abc",
      "brand": "xyz",
      "information": {
      "c1": "101",
      "c2": "11111",
      "c3": "a,b,c,d,e:abc."
      }
      },
      {
      "Title": "RX100",
      "brand": "Yamaha",
      "information": {
      "c1": "102",
      "c2": "22222",
      "c3": "a."
      }
      },
      {
      "Title": "victor",
      "brand": "TVS",
      "information": {
      "c1": "103",
      "c2": "33333",
      "c3": "a,b,c"
      }
      },
      {
      "Title": "R15",
      "brand": "Yamaha",
      "information": {
      "c1": "104",
      "c2": "44444",
      "c3": "a,b"
      }
      }
      ]
      }


      I have successfully downloaded and converted into multiple csv on the basis of number of Title.
      Data looks like in the below form after converting in multiple csv file.



      Headers-> c1,c2,c3
      csv1--> 101,11111,a,b,c,d,e:abc.
      csv2--> 102,22222,a.
      csv3--> 103,33333,a,b,c.
      csv4--> 104,44444,a,b.


      but I want the above data in below form.



      Headers-> c1,c2,c3,c4,c5,c6,c7
      csv1--> 101,11111,a,b,c,d,e:abc.
      csv2--> 102,22222,a.
      csv3--> 103,33333,a,b,c.
      csv4--> 104,44444,a,b.


      Is it possible to divide the c3 into different number of columns on the basis of "," present in my json file using json. The column c3 will get divided on the basis of number of elements are present and will be the maximum value of c3 data.










      share|improve this question














      I am downloading api json data and converting into my final result as a csv file.



      The sample data is in the below form:



      {
      "content": [{
      "Title": "abc",
      "brand": "xyz",
      "information": {
      "c1": "101",
      "c2": "11111",
      "c3": "a,b,c,d,e:abc."
      }
      },
      {
      "Title": "RX100",
      "brand": "Yamaha",
      "information": {
      "c1": "102",
      "c2": "22222",
      "c3": "a."
      }
      },
      {
      "Title": "victor",
      "brand": "TVS",
      "information": {
      "c1": "103",
      "c2": "33333",
      "c3": "a,b,c"
      }
      },
      {
      "Title": "R15",
      "brand": "Yamaha",
      "information": {
      "c1": "104",
      "c2": "44444",
      "c3": "a,b"
      }
      }
      ]
      }


      I have successfully downloaded and converted into multiple csv on the basis of number of Title.
      Data looks like in the below form after converting in multiple csv file.



      Headers-> c1,c2,c3
      csv1--> 101,11111,a,b,c,d,e:abc.
      csv2--> 102,22222,a.
      csv3--> 103,33333,a,b,c.
      csv4--> 104,44444,a,b.


      but I want the above data in below form.



      Headers-> c1,c2,c3,c4,c5,c6,c7
      csv1--> 101,11111,a,b,c,d,e:abc.
      csv2--> 102,22222,a.
      csv3--> 103,33333,a,b,c.
      csv4--> 104,44444,a,b.


      Is it possible to divide the c3 into different number of columns on the basis of "," present in my json file using json. The column c3 will get divided on the basis of number of elements are present and will be the maximum value of c3 data.







      shell-script jq






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      samsam

      386




      386






















          1 Answer
          1






          active

          oldest

          votes


















          0














          For this exact data,



          jq -r '.content.information | [.c1, .c2, .c3] | join(",")' < sample.json


          will work - just inserting commas between the three columns replicates the effect of a joined-upCSV file. For more complex real data, particularly with numbers, this will work better:



          jq -r '.content.information | [.c1, .c2, .c3|split(",")|.] | @csv' < sample.json


          This one is all fairly standard up to .c3|split(",")|., which




          1. Extracts column 3's value

          2. Splits that value into an array on commas

          3. Flattens that array out


          @csv then converts the array into CSV format. This produces the following output for your sample file:



          "101","11111","a","b","c","d","e:abc."
          "102","22222","a."
          "103","33333","a","b","c"
          "104","44444","a","b"


          If you don't want quotes, the most reliable thing is to use @tsv and just replace the tabs:



          jq -r '.content.information|[.c1, .c2, .c3|split(",")|.] | @tsv|gsub("t"; ",")' < sample.json


          If all your values are strings you could also use join(",") again.





          If you want the literal header line c1,...c7 created as well, this will do it:



          jq -r '[.content.information|[.c1, .c2, .c3|split(",")|.]] | (([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")), (. | join(",")))' < sample.json


          There's three parts: the first creates the arrays of columns as before, and then the other two parts use that as input:





          1. ([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")) generates the header line: it finds the maximum length of any row by generating an array of row lengths, creates a range 0..maximum and maps over it to create an array of "c1".."c7", then joins them all together with commas.


          2. (. | join(",")) is analogous to our second half from earlier, and the same things about @csv apply.






          share|improve this answer

























            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%2f507615%2fparse-json-data-using-jq-and-convert-into-csv%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









            0














            For this exact data,



            jq -r '.content.information | [.c1, .c2, .c3] | join(",")' < sample.json


            will work - just inserting commas between the three columns replicates the effect of a joined-upCSV file. For more complex real data, particularly with numbers, this will work better:



            jq -r '.content.information | [.c1, .c2, .c3|split(",")|.] | @csv' < sample.json


            This one is all fairly standard up to .c3|split(",")|., which




            1. Extracts column 3's value

            2. Splits that value into an array on commas

            3. Flattens that array out


            @csv then converts the array into CSV format. This produces the following output for your sample file:



            "101","11111","a","b","c","d","e:abc."
            "102","22222","a."
            "103","33333","a","b","c"
            "104","44444","a","b"


            If you don't want quotes, the most reliable thing is to use @tsv and just replace the tabs:



            jq -r '.content.information|[.c1, .c2, .c3|split(",")|.] | @tsv|gsub("t"; ",")' < sample.json


            If all your values are strings you could also use join(",") again.





            If you want the literal header line c1,...c7 created as well, this will do it:



            jq -r '[.content.information|[.c1, .c2, .c3|split(",")|.]] | (([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")), (. | join(",")))' < sample.json


            There's three parts: the first creates the arrays of columns as before, and then the other two parts use that as input:





            1. ([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")) generates the header line: it finds the maximum length of any row by generating an array of row lengths, creates a range 0..maximum and maps over it to create an array of "c1".."c7", then joins them all together with commas.


            2. (. | join(",")) is analogous to our second half from earlier, and the same things about @csv apply.






            share|improve this answer






























              0














              For this exact data,



              jq -r '.content.information | [.c1, .c2, .c3] | join(",")' < sample.json


              will work - just inserting commas between the three columns replicates the effect of a joined-upCSV file. For more complex real data, particularly with numbers, this will work better:



              jq -r '.content.information | [.c1, .c2, .c3|split(",")|.] | @csv' < sample.json


              This one is all fairly standard up to .c3|split(",")|., which




              1. Extracts column 3's value

              2. Splits that value into an array on commas

              3. Flattens that array out


              @csv then converts the array into CSV format. This produces the following output for your sample file:



              "101","11111","a","b","c","d","e:abc."
              "102","22222","a."
              "103","33333","a","b","c"
              "104","44444","a","b"


              If you don't want quotes, the most reliable thing is to use @tsv and just replace the tabs:



              jq -r '.content.information|[.c1, .c2, .c3|split(",")|.] | @tsv|gsub("t"; ",")' < sample.json


              If all your values are strings you could also use join(",") again.





              If you want the literal header line c1,...c7 created as well, this will do it:



              jq -r '[.content.information|[.c1, .c2, .c3|split(",")|.]] | (([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")), (. | join(",")))' < sample.json


              There's three parts: the first creates the arrays of columns as before, and then the other two parts use that as input:





              1. ([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")) generates the header line: it finds the maximum length of any row by generating an array of row lengths, creates a range 0..maximum and maps over it to create an array of "c1".."c7", then joins them all together with commas.


              2. (. | join(",")) is analogous to our second half from earlier, and the same things about @csv apply.






              share|improve this answer




























                0












                0








                0







                For this exact data,



                jq -r '.content.information | [.c1, .c2, .c3] | join(",")' < sample.json


                will work - just inserting commas between the three columns replicates the effect of a joined-upCSV file. For more complex real data, particularly with numbers, this will work better:



                jq -r '.content.information | [.c1, .c2, .c3|split(",")|.] | @csv' < sample.json


                This one is all fairly standard up to .c3|split(",")|., which




                1. Extracts column 3's value

                2. Splits that value into an array on commas

                3. Flattens that array out


                @csv then converts the array into CSV format. This produces the following output for your sample file:



                "101","11111","a","b","c","d","e:abc."
                "102","22222","a."
                "103","33333","a","b","c"
                "104","44444","a","b"


                If you don't want quotes, the most reliable thing is to use @tsv and just replace the tabs:



                jq -r '.content.information|[.c1, .c2, .c3|split(",")|.] | @tsv|gsub("t"; ",")' < sample.json


                If all your values are strings you could also use join(",") again.





                If you want the literal header line c1,...c7 created as well, this will do it:



                jq -r '[.content.information|[.c1, .c2, .c3|split(",")|.]] | (([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")), (. | join(",")))' < sample.json


                There's three parts: the first creates the arrays of columns as before, and then the other two parts use that as input:





                1. ([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")) generates the header line: it finds the maximum length of any row by generating an array of row lengths, creates a range 0..maximum and maps over it to create an array of "c1".."c7", then joins them all together with commas.


                2. (. | join(",")) is analogous to our second half from earlier, and the same things about @csv apply.






                share|improve this answer















                For this exact data,



                jq -r '.content.information | [.c1, .c2, .c3] | join(",")' < sample.json


                will work - just inserting commas between the three columns replicates the effect of a joined-upCSV file. For more complex real data, particularly with numbers, this will work better:



                jq -r '.content.information | [.c1, .c2, .c3|split(",")|.] | @csv' < sample.json


                This one is all fairly standard up to .c3|split(",")|., which




                1. Extracts column 3's value

                2. Splits that value into an array on commas

                3. Flattens that array out


                @csv then converts the array into CSV format. This produces the following output for your sample file:



                "101","11111","a","b","c","d","e:abc."
                "102","22222","a."
                "103","33333","a","b","c"
                "104","44444","a","b"


                If you don't want quotes, the most reliable thing is to use @tsv and just replace the tabs:



                jq -r '.content.information|[.c1, .c2, .c3|split(",")|.] | @tsv|gsub("t"; ",")' < sample.json


                If all your values are strings you could also use join(",") again.





                If you want the literal header line c1,...c7 created as well, this will do it:



                jq -r '[.content.information|[.c1, .c2, .c3|split(",")|.]] | (([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")), (. | join(",")))' < sample.json


                There's three parts: the first creates the arrays of columns as before, and then the other two parts use that as input:





                1. ([range([. | length] | max)|"c" + (.+1|tostring)]|join(",")) generates the header line: it finds the maximum length of any row by generating an array of row lengths, creates a range 0..maximum and maps over it to create an array of "c1".."c7", then joins them all together with commas.


                2. (. | join(",")) is analogous to our second half from earlier, and the same things about @csv apply.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 38 mins ago

























                answered 45 mins ago









                Michael HomerMichael Homer

                49.9k8137175




                49.9k8137175






























                    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%2f507615%2fparse-json-data-using-jq-and-convert-into-csv%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

                    濃尾地震

                    How to rewrite equation of hyperbola in standard form

                    No ethernet ip address in my vocore2