Convert json to ruby hash












121















I have a JSON object holding the following value:



@value = {"val":"test","val1":"test1","val2":"test2"}


I want to loop through it in Ruby to get the key value pairs. When I use @each, it doesn't iterate through the object because it is not in the ruby hash form:



@value = {"val"=>"test","val1"=>"test1","val2"=>"test2"}


How can I convert the above JSON object to Ruby hash?










share|improve this question





























    121















    I have a JSON object holding the following value:



    @value = {"val":"test","val1":"test1","val2":"test2"}


    I want to loop through it in Ruby to get the key value pairs. When I use @each, it doesn't iterate through the object because it is not in the ruby hash form:



    @value = {"val"=>"test","val1"=>"test1","val2"=>"test2"}


    How can I convert the above JSON object to Ruby hash?










    share|improve this question



























      121












      121








      121


      20






      I have a JSON object holding the following value:



      @value = {"val":"test","val1":"test1","val2":"test2"}


      I want to loop through it in Ruby to get the key value pairs. When I use @each, it doesn't iterate through the object because it is not in the ruby hash form:



      @value = {"val"=>"test","val1"=>"test1","val2"=>"test2"}


      How can I convert the above JSON object to Ruby hash?










      share|improve this question
















      I have a JSON object holding the following value:



      @value = {"val":"test","val1":"test1","val2":"test2"}


      I want to loop through it in Ruby to get the key value pairs. When I use @each, it doesn't iterate through the object because it is not in the ruby hash form:



      @value = {"val"=>"test","val1"=>"test1","val2"=>"test2"}


      How can I convert the above JSON object to Ruby hash?







      ruby json hashmap






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 21 '17 at 14:56









      Tot Zam

      3,92443354




      3,92443354










      asked Nov 1 '11 at 8:57









      verdureverdure

      1,23662026




      1,23662026
























          5 Answers
          5






          active

          oldest

          votes


















          230














          What about the following snippet?



          require 'json'
          value = '{"val":"test","val1":"test1","val2":"test2"}'
          puts JSON.parse(value) # => {"val"=>"test","val1"=>"test1","val2"=>"test2"}





          share|improve this answer





















          • 7





            value = '{"val":"test","val1":"test1","val2":"test2"}' could have been more readable.

            – luckykrrish
            Oct 23 '12 at 11:34





















          19














          You could also use ruby's method: with_indifferent_access so you could access the body with either symbols or strings.



          value = '{"val":"test","val1":"test1","val2":"test2"}'
          json = JSON.parse(value).with_indifferent_access


          then



          json[:val] #=> "test"

          json["val"] #=> "test"





          share|improve this answer
























          • thanks for .with_indifferent_access

            – Anton Semenichenko
            Apr 4 '18 at 13:11













          • Does anyone know if this is more resource-intensive for larger hash objects? I'm new to Ruby/Rails, but assuming this duplicates key-value pairs?

            – Jonathan
            Apr 9 '18 at 11:52



















          3














          Assuming you have a JSON hash hanging around somewhere, to automatically convert it into something like WarHog's version, wrap your json hash contents in %q{hsh} tags. This seems to automatically add all the necessary escaped text like in WarHog's answer






          share|improve this answer































            2














            Have you tried: http://flori.github.com/json/.



            Failing that, you could just parse it out? If it's only arrays you're interested in, something to split the above out will be quite simple.
            N






            share|improve this answer































              0














              You can use the nice_hash gem: https://github.com/MarioRuiz/nice_hash



              require 'nice_hash'
              my_string = '{"val":"test","val1":"test1","val2":"test2"}'

              # on my_hash will have the json as a hash, even when nested with arrays
              my_hash = my_string.json

              # you can filter and get what you want even when nested with arrays
              vals = my_string.json(:val1, :val2)

              # even you can access the keys like this:
              puts my_hash._val1
              puts my_hash.val1
              puts my_hash[:val1]





              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%2f7964282%2fconvert-json-to-ruby-hash%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                230














                What about the following snippet?



                require 'json'
                value = '{"val":"test","val1":"test1","val2":"test2"}'
                puts JSON.parse(value) # => {"val"=>"test","val1"=>"test1","val2"=>"test2"}





                share|improve this answer





















                • 7





                  value = '{"val":"test","val1":"test1","val2":"test2"}' could have been more readable.

                  – luckykrrish
                  Oct 23 '12 at 11:34


















                230














                What about the following snippet?



                require 'json'
                value = '{"val":"test","val1":"test1","val2":"test2"}'
                puts JSON.parse(value) # => {"val"=>"test","val1"=>"test1","val2"=>"test2"}





                share|improve this answer





















                • 7





                  value = '{"val":"test","val1":"test1","val2":"test2"}' could have been more readable.

                  – luckykrrish
                  Oct 23 '12 at 11:34
















                230












                230








                230







                What about the following snippet?



                require 'json'
                value = '{"val":"test","val1":"test1","val2":"test2"}'
                puts JSON.parse(value) # => {"val"=>"test","val1"=>"test1","val2"=>"test2"}





                share|improve this answer















                What about the following snippet?



                require 'json'
                value = '{"val":"test","val1":"test1","val2":"test2"}'
                puts JSON.parse(value) # => {"val"=>"test","val1"=>"test1","val2"=>"test2"}






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 5 '14 at 12:31









                Sergey Alekseev

                5,56532544




                5,56532544










                answered Nov 1 '11 at 9:08









                WarHogWarHog

                7,84522223




                7,84522223








                • 7





                  value = '{"val":"test","val1":"test1","val2":"test2"}' could have been more readable.

                  – luckykrrish
                  Oct 23 '12 at 11:34
















                • 7





                  value = '{"val":"test","val1":"test1","val2":"test2"}' could have been more readable.

                  – luckykrrish
                  Oct 23 '12 at 11:34










                7




                7





                value = '{"val":"test","val1":"test1","val2":"test2"}' could have been more readable.

                – luckykrrish
                Oct 23 '12 at 11:34







                value = '{"val":"test","val1":"test1","val2":"test2"}' could have been more readable.

                – luckykrrish
                Oct 23 '12 at 11:34















                19














                You could also use ruby's method: with_indifferent_access so you could access the body with either symbols or strings.



                value = '{"val":"test","val1":"test1","val2":"test2"}'
                json = JSON.parse(value).with_indifferent_access


                then



                json[:val] #=> "test"

                json["val"] #=> "test"





                share|improve this answer
























                • thanks for .with_indifferent_access

                  – Anton Semenichenko
                  Apr 4 '18 at 13:11













                • Does anyone know if this is more resource-intensive for larger hash objects? I'm new to Ruby/Rails, but assuming this duplicates key-value pairs?

                  – Jonathan
                  Apr 9 '18 at 11:52
















                19














                You could also use ruby's method: with_indifferent_access so you could access the body with either symbols or strings.



                value = '{"val":"test","val1":"test1","val2":"test2"}'
                json = JSON.parse(value).with_indifferent_access


                then



                json[:val] #=> "test"

                json["val"] #=> "test"





                share|improve this answer
























                • thanks for .with_indifferent_access

                  – Anton Semenichenko
                  Apr 4 '18 at 13:11













                • Does anyone know if this is more resource-intensive for larger hash objects? I'm new to Ruby/Rails, but assuming this duplicates key-value pairs?

                  – Jonathan
                  Apr 9 '18 at 11:52














                19












                19








                19







                You could also use ruby's method: with_indifferent_access so you could access the body with either symbols or strings.



                value = '{"val":"test","val1":"test1","val2":"test2"}'
                json = JSON.parse(value).with_indifferent_access


                then



                json[:val] #=> "test"

                json["val"] #=> "test"





                share|improve this answer













                You could also use ruby's method: with_indifferent_access so you could access the body with either symbols or strings.



                value = '{"val":"test","val1":"test1","val2":"test2"}'
                json = JSON.parse(value).with_indifferent_access


                then



                json[:val] #=> "test"

                json["val"] #=> "test"






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 30 '17 at 17:42









                crims345crims345

                31934




                31934













                • thanks for .with_indifferent_access

                  – Anton Semenichenko
                  Apr 4 '18 at 13:11













                • Does anyone know if this is more resource-intensive for larger hash objects? I'm new to Ruby/Rails, but assuming this duplicates key-value pairs?

                  – Jonathan
                  Apr 9 '18 at 11:52



















                • thanks for .with_indifferent_access

                  – Anton Semenichenko
                  Apr 4 '18 at 13:11













                • Does anyone know if this is more resource-intensive for larger hash objects? I'm new to Ruby/Rails, but assuming this duplicates key-value pairs?

                  – Jonathan
                  Apr 9 '18 at 11:52

















                thanks for .with_indifferent_access

                – Anton Semenichenko
                Apr 4 '18 at 13:11







                thanks for .with_indifferent_access

                – Anton Semenichenko
                Apr 4 '18 at 13:11















                Does anyone know if this is more resource-intensive for larger hash objects? I'm new to Ruby/Rails, but assuming this duplicates key-value pairs?

                – Jonathan
                Apr 9 '18 at 11:52





                Does anyone know if this is more resource-intensive for larger hash objects? I'm new to Ruby/Rails, but assuming this duplicates key-value pairs?

                – Jonathan
                Apr 9 '18 at 11:52











                3














                Assuming you have a JSON hash hanging around somewhere, to automatically convert it into something like WarHog's version, wrap your json hash contents in %q{hsh} tags. This seems to automatically add all the necessary escaped text like in WarHog's answer






                share|improve this answer




























                  3














                  Assuming you have a JSON hash hanging around somewhere, to automatically convert it into something like WarHog's version, wrap your json hash contents in %q{hsh} tags. This seems to automatically add all the necessary escaped text like in WarHog's answer






                  share|improve this answer


























                    3












                    3








                    3







                    Assuming you have a JSON hash hanging around somewhere, to automatically convert it into something like WarHog's version, wrap your json hash contents in %q{hsh} tags. This seems to automatically add all the necessary escaped text like in WarHog's answer






                    share|improve this answer













                    Assuming you have a JSON hash hanging around somewhere, to automatically convert it into something like WarHog's version, wrap your json hash contents in %q{hsh} tags. This seems to automatically add all the necessary escaped text like in WarHog's answer







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 15 '12 at 21:22









                    boulder_rubyboulder_ruby

                    27.1k65875




                    27.1k65875























                        2














                        Have you tried: http://flori.github.com/json/.



                        Failing that, you could just parse it out? If it's only arrays you're interested in, something to split the above out will be quite simple.
                        N






                        share|improve this answer




























                          2














                          Have you tried: http://flori.github.com/json/.



                          Failing that, you could just parse it out? If it's only arrays you're interested in, something to split the above out will be quite simple.
                          N






                          share|improve this answer


























                            2












                            2








                            2







                            Have you tried: http://flori.github.com/json/.



                            Failing that, you could just parse it out? If it's only arrays you're interested in, something to split the above out will be quite simple.
                            N






                            share|improve this answer













                            Have you tried: http://flori.github.com/json/.



                            Failing that, you could just parse it out? If it's only arrays you're interested in, something to split the above out will be quite simple.
                            N







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 1 '11 at 8:59









                            Nick CartwrightNick Cartwright

                            4,526133956




                            4,526133956























                                0














                                You can use the nice_hash gem: https://github.com/MarioRuiz/nice_hash



                                require 'nice_hash'
                                my_string = '{"val":"test","val1":"test1","val2":"test2"}'

                                # on my_hash will have the json as a hash, even when nested with arrays
                                my_hash = my_string.json

                                # you can filter and get what you want even when nested with arrays
                                vals = my_string.json(:val1, :val2)

                                # even you can access the keys like this:
                                puts my_hash._val1
                                puts my_hash.val1
                                puts my_hash[:val1]





                                share|improve this answer




























                                  0














                                  You can use the nice_hash gem: https://github.com/MarioRuiz/nice_hash



                                  require 'nice_hash'
                                  my_string = '{"val":"test","val1":"test1","val2":"test2"}'

                                  # on my_hash will have the json as a hash, even when nested with arrays
                                  my_hash = my_string.json

                                  # you can filter and get what you want even when nested with arrays
                                  vals = my_string.json(:val1, :val2)

                                  # even you can access the keys like this:
                                  puts my_hash._val1
                                  puts my_hash.val1
                                  puts my_hash[:val1]





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    You can use the nice_hash gem: https://github.com/MarioRuiz/nice_hash



                                    require 'nice_hash'
                                    my_string = '{"val":"test","val1":"test1","val2":"test2"}'

                                    # on my_hash will have the json as a hash, even when nested with arrays
                                    my_hash = my_string.json

                                    # you can filter and get what you want even when nested with arrays
                                    vals = my_string.json(:val1, :val2)

                                    # even you can access the keys like this:
                                    puts my_hash._val1
                                    puts my_hash.val1
                                    puts my_hash[:val1]





                                    share|improve this answer













                                    You can use the nice_hash gem: https://github.com/MarioRuiz/nice_hash



                                    require 'nice_hash'
                                    my_string = '{"val":"test","val1":"test1","val2":"test2"}'

                                    # on my_hash will have the json as a hash, even when nested with arrays
                                    my_hash = my_string.json

                                    # you can filter and get what you want even when nested with arrays
                                    vals = my_string.json(:val1, :val2)

                                    # even you can access the keys like this:
                                    puts my_hash._val1
                                    puts my_hash.val1
                                    puts my_hash[:val1]






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 2 at 14:46









                                    Mario RuizMario Ruiz

                                    346




                                    346






























                                        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%2f7964282%2fconvert-json-to-ruby-hash%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

                                        Monofisismo

                                        Angular Downloading a file using contenturl with Basic Authentication

                                        Olmecas