Convert json to ruby hash
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
add a comment |
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
add a comment |
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
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
ruby json hashmap
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
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
What about the following snippet?
require 'json'
value = '{"val":"test","val1":"test1","val2":"test2"}'
puts JSON.parse(value) # => {"val"=>"test","val1"=>"test1","val2"=>"test2"}
7
value = '{"val":"test","val1":"test1","val2":"test2"}'
could have been more readable.
– luckykrrish
Oct 23 '12 at 11:34
add a comment |
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"
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
add a comment |
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
add a comment |
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
add a comment |
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]
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
What about the following snippet?
require 'json'
value = '{"val":"test","val1":"test1","val2":"test2"}'
puts JSON.parse(value) # => {"val"=>"test","val1"=>"test1","val2"=>"test2"}
7
value = '{"val":"test","val1":"test1","val2":"test2"}'
could have been more readable.
– luckykrrish
Oct 23 '12 at 11:34
add a comment |
What about the following snippet?
require 'json'
value = '{"val":"test","val1":"test1","val2":"test2"}'
puts JSON.parse(value) # => {"val"=>"test","val1"=>"test1","val2"=>"test2"}
7
value = '{"val":"test","val1":"test1","val2":"test2"}'
could have been more readable.
– luckykrrish
Oct 23 '12 at 11:34
add a comment |
What about the following snippet?
require 'json'
value = '{"val":"test","val1":"test1","val2":"test2"}'
puts JSON.parse(value) # => {"val"=>"test","val1"=>"test1","val2"=>"test2"}
What about the following snippet?
require 'json'
value = '{"val":"test","val1":"test1","val2":"test2"}'
puts JSON.parse(value) # => {"val"=>"test","val1"=>"test1","val2"=>"test2"}
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
add a comment |
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
add a comment |
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"
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
add a comment |
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"
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
add a comment |
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"
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"
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jul 15 '12 at 21:22
boulder_rubyboulder_ruby
27.1k65875
27.1k65875
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 1 '11 at 8:59
Nick CartwrightNick Cartwright
4,526133956
4,526133956
add a comment |
add a comment |
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]
add a comment |
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]
add a comment |
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]
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]
answered Jan 2 at 14:46
Mario RuizMario Ruiz
346
346
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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