How do I parse JSON with Ruby on Rails

0 votes

I'm looking for a simple way to parse JSON, extract a value and write it into a database in Rails.

Specifically what I'm looking for, is a way to extract shortUrl from the JSON returned from the bit.ly API:

{
  "errorCode": 0,
  "errorMessage": "",
  "results":
  {
    "http://www.foo.com":
    {
       "hash": "e5TEd",
       "shortKeywordUrl": "",
       "shortUrl": "http://bit.ly/1a0p8G",
       "userHash": "1a0p8G"
    }
  },
  "statusCode": "OK"
}

And then take that shortUrl and write it into an ActiveRecord object associated with the long URL.

Jul 25, 2020 in Java-Script by kartik
• 37,510 points
4,109 views

1 answer to this question.

0 votes

Hello @kartik,

Use 

hash = JSON.parse string

Rails should automagically load the json module for you, so you don't need to add require 'json'.

Hope it works!!

Thank You!!

answered Jul 25, 2020 by Niroj
• 82,880 points

Related Questions In Java-Script

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,880 points
755 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,880 points
3,689 views
0 votes
1 answer

How do I select an element with its name attribute in jQuery?

Hello @kartik, You can use: jQuery('[name="' + nameAttributeValue + ...READ MORE

answered Jun 11, 2020 in Java-Script by Niroj
• 82,880 points
804 views
0 votes
1 answer

How do I run PHP code when a user clicks on a link?

Hello @kartik, You'd need to have a javascript ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,880 points
8,749 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,880 points
2,685 views
0 votes
1 answer

How to check if a specific key is present in a hash or not?

Hello @kartik, While Hash#has_key? gets the job done, as it has ...READ MORE

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

How to drop columns using Rails migration?

Hello @kartik, Use this: remove_column :table_name, :column_name For instance: remove_column :users, ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,880 points
804 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,880 points
3,280 views
0 votes
1 answer

How can I rename a database column in a Ruby on Rails migration?

Hello @kartik, Try using this: rename_column :table, :old_column, :new_column You'll ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,880 points
1,339 views
0 votes
1 answer

How to “pretty” format JSON output in Ruby on Rails?

Hello @kartik, Use the pretty_generate() function, built into later versions ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,880 points
10,345 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