How to convert a ruby hash object to JSON

0 votes

How to convert a ruby hash object to JSON? So I am trying this example below & it doesn't work?

I was looking at the RubyDoc and obviously Hash object doesn't have a to_json method. But I am reading on blogs that Rails supports active_record.to_json and also supports hash#to_json. I can understand ActiveRecord is a Rails object, but Hash is not native to Rails, it's a pure Ruby object. So in Rails you can do a hash.to_json, but not in pure Ruby??

car = {:make => "bmw", :year => "2020"}
car.to_json
Jul 24, 2020 in Java-Script by kartik
• 37,520 points
12,667 views

1 answer to this question.

0 votes

Hello @kartik,

You can also use JSON.generate:

require 'json'

JSON.generate({ foo: "bar" })
=> "{\"foo\":\"bar\"}"

Or its alias, JSON.unparse:

require 'json'

JSON.unparse({ foo: "bar" })
=> "{\"foo\":\"bar\"}"

Hope it helps!!

Thank You!

answered Jul 24, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
1 answer

How to print a circular structure in a JSON-like format?

Hello, You can use util.inspect(object) in node.js. It automatically ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,840 points
2,765 views
0 votes
1 answer

How do I turn a string to a json in Node.js?

Hello Kartik, Use the JSON function  JSON.parse(theString) ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,840 points
975 views
0 votes
1 answer

How to list the properties of a JavaScript object?

Hii @kartik, Use Reflect.ownKeys(): var obj = {a: 1, b: ...READ MORE

answered Jun 8, 2020 in Java-Script by Niroj
• 82,840 points
1,015 views
0 votes
1 answer

How to convert HTML to JSON using PHP?

Hello @kartik, If you are able to obtain ...READ MORE

answered Jun 19, 2020 in Java-Script by Niroj
• 82,840 points
7,716 views
0 votes
1 answer

How to rollback a specific migration?

Hello @kartik, Try this: rake db:migrate:down VERSION=20100905201547 will roll back ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,840 points
3,115 views
0 votes
1 answer

How can I specify a local gem in my Gemfile?

Hello @kartik, In order to use local gem ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,840 points
3,686 views
0 votes
1 answer

Error:bundle install fails with SSL certificate verification error

Hello @kartik, Replace the ssl gem source with ...READ MORE

answered Jul 25, 2020 in Java-Script by Niroj
• 82,840 points
1,802 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

How to deserializing a JSON into a JavaScript object?

Hello @kartik, To collect all item of an array ...READ MORE

answered Sep 3, 2020 in Java-Script by Niroj
• 82,840 points
779 views
0 votes
1 answer

How to clone a JavaScript object?

Hello, You can clone an object and remove ...READ MORE

answered Apr 2, 2020 in Java-Script by Niroj
• 82,840 points
930 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP