I was able to do it using to_json
Parsing Hashie::Mash output to Json in ruby
-
30-06-2022 - |
题
I'm getting user input in JSON format for an API created using grape. In a particular parameter, I'm giving an array of JSON in the following format.
"personal" : {
"details" : {
"firstname" :"nagalakshmi",
"lastname" : "n"
}
}
When I tried to print "personal" attribute it is showing like below
#<Hashie::Mash details=#<Hashie::Mash firstname="nagalakshmi" lastname="n">>
Is there any way to parse the attribute to json format?
解决方案
其他提示
I also got this same object as params with Grape.
Hashie::Mash
params: #<Hashie::Mash data=#<Hashie::Mash attributes=#<Hashie::Mash title="Ember Hamster"> type="pictures">>
If you are using Rails We can convert into ruby hash as following:
params.to_hash
gives
{"data"=>{"type"=>"pictures", "attributes"=>{"title"=>"Ember Hamster"}}}
不隶属于 StackOverflow