Convert an array into a Map












6














I have a table with a column like



[{"key":"e","value":["253","203","204"]},{"key":"st","value":["mi"]},{"key":"k2","value":["1","2"]}]


Which is of the format array<struct<key:string,value:array<string>>>



I want to convert the column into below format :



{"e":["253","203","204"],"st":["mi"],"k2":["1","2"]}


which is of the type map<string,array<string>>



I have tried exploding the array but that does not work. Any ideas how I can do this in hive.










share|improve this question




















  • 1




    I think you will have to write the custom UDF for this. you you can use spark
    – Gaurang Shah
    Nov 7 '18 at 20:01






  • 2




    Your output json is invalid. Don't you think for Map<String,List<String>> should be represented by {"e":["253","203","204"],"st":["mi"],"k2":["1","2"]} .
    – apandey846
    Nov 11 '18 at 19:59
















6














I have a table with a column like



[{"key":"e","value":["253","203","204"]},{"key":"st","value":["mi"]},{"key":"k2","value":["1","2"]}]


Which is of the format array<struct<key:string,value:array<string>>>



I want to convert the column into below format :



{"e":["253","203","204"],"st":["mi"],"k2":["1","2"]}


which is of the type map<string,array<string>>



I have tried exploding the array but that does not work. Any ideas how I can do this in hive.










share|improve this question




















  • 1




    I think you will have to write the custom UDF for this. you you can use spark
    – Gaurang Shah
    Nov 7 '18 at 20:01






  • 2




    Your output json is invalid. Don't you think for Map<String,List<String>> should be represented by {"e":["253","203","204"],"st":["mi"],"k2":["1","2"]} .
    – apandey846
    Nov 11 '18 at 19:59














6












6








6


1





I have a table with a column like



[{"key":"e","value":["253","203","204"]},{"key":"st","value":["mi"]},{"key":"k2","value":["1","2"]}]


Which is of the format array<struct<key:string,value:array<string>>>



I want to convert the column into below format :



{"e":["253","203","204"],"st":["mi"],"k2":["1","2"]}


which is of the type map<string,array<string>>



I have tried exploding the array but that does not work. Any ideas how I can do this in hive.










share|improve this question















I have a table with a column like



[{"key":"e","value":["253","203","204"]},{"key":"st","value":["mi"]},{"key":"k2","value":["1","2"]}]


Which is of the format array<struct<key:string,value:array<string>>>



I want to convert the column into below format :



{"e":["253","203","204"],"st":["mi"],"k2":["1","2"]}


which is of the type map<string,array<string>>



I have tried exploding the array but that does not work. Any ideas how I can do this in hive.







sql hadoop hive hiveql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 4:50









apandey846

8441019




8441019










asked Nov 7 '18 at 16:48









John Constantine

1417




1417








  • 1




    I think you will have to write the custom UDF for this. you you can use spark
    – Gaurang Shah
    Nov 7 '18 at 20:01






  • 2




    Your output json is invalid. Don't you think for Map<String,List<String>> should be represented by {"e":["253","203","204"],"st":["mi"],"k2":["1","2"]} .
    – apandey846
    Nov 11 '18 at 19:59














  • 1




    I think you will have to write the custom UDF for this. you you can use spark
    – Gaurang Shah
    Nov 7 '18 at 20:01






  • 2




    Your output json is invalid. Don't you think for Map<String,List<String>> should be represented by {"e":["253","203","204"],"st":["mi"],"k2":["1","2"]} .
    – apandey846
    Nov 11 '18 at 19:59








1




1




I think you will have to write the custom UDF for this. you you can use spark
– Gaurang Shah
Nov 7 '18 at 20:01




I think you will have to write the custom UDF for this. you you can use spark
– Gaurang Shah
Nov 7 '18 at 20:01




2




2




Your output json is invalid. Don't you think for Map<String,List<String>> should be represented by {"e":["253","203","204"],"st":["mi"],"k2":["1","2"]} .
– apandey846
Nov 11 '18 at 19:59




Your output json is invalid. Don't you think for Map<String,List<String>> should be represented by {"e":["253","203","204"],"st":["mi"],"k2":["1","2"]} .
– apandey846
Nov 11 '18 at 19:59












1 Answer
1






active

oldest

votes


















0














var Array = [{"key":"e","value":["253","203","204"]},{"key":"st","value":["mi"]},{"key":"k2","value":["1","2"]}];

var obj = {}
for(var i=0;i<Array.length;i++){
obj[Array[i].key] = Array[i].value
}


obj will be in the required format






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%2f53194093%2fconvert-an-array-into-a-map%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














    var Array = [{"key":"e","value":["253","203","204"]},{"key":"st","value":["mi"]},{"key":"k2","value":["1","2"]}];

    var obj = {}
    for(var i=0;i<Array.length;i++){
    obj[Array[i].key] = Array[i].value
    }


    obj will be in the required format






    share|improve this answer




























      0














      var Array = [{"key":"e","value":["253","203","204"]},{"key":"st","value":["mi"]},{"key":"k2","value":["1","2"]}];

      var obj = {}
      for(var i=0;i<Array.length;i++){
      obj[Array[i].key] = Array[i].value
      }


      obj will be in the required format






      share|improve this answer


























        0












        0








        0






        var Array = [{"key":"e","value":["253","203","204"]},{"key":"st","value":["mi"]},{"key":"k2","value":["1","2"]}];

        var obj = {}
        for(var i=0;i<Array.length;i++){
        obj[Array[i].key] = Array[i].value
        }


        obj will be in the required format






        share|improve this answer














        var Array = [{"key":"e","value":["253","203","204"]},{"key":"st","value":["mi"]},{"key":"k2","value":["1","2"]}];

        var obj = {}
        for(var i=0;i<Array.length;i++){
        obj[Array[i].key] = Array[i].value
        }


        obj will be in the required format







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 18 '18 at 7:29

























        answered Nov 18 '18 at 7:16









        Habeeb Rahman

        12




        12






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53194093%2fconvert-an-array-into-a-map%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

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation