Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7634503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:11:39+00:00 2026-05-31T07:11:39+00:00

I am trying to query an api through JSON / REST using Ruby. require

  • 0

I am trying to query an api through JSON / REST using Ruby.

require 'rubygems'
require 'rest-client'
require 'json'


###Request Build#####
url = 'http://site_name'
request ={
     "format"=>'json',
     "foo"=> {"first"=>1.1,"second"=>2.2},
     "foo_1"=>300,
     "foo_2"=>"speed",
     "foo_3"=>[
         {"id"=> "abc123", "first"=> 1.8, "second"=> 2.8},
         {"id"=> "abc456", "first"=> -1.5, "second"=> 1.2}
         ]
}.to_json

### go go go ###
response = RestClient.post(url,request, :content_type => :json, :accept => :json)
puts response

The above works, it will query the api just fine. However the API documentation I am using says I should have “:” instead of “=>” like this

     "format":'json',
     "foo":{"first":1.1,"second":2.2},
     "foo_1":300,
     "foo_2":"speed",
     "foo_3":[
         {"id":"abc123", "first":1.8, "second":2.8},
         {"id":"abc456", "first":-1.5, "second":1.2}
         ]
}

when I do use them I get this error:

new.rb:10: syntax error, unexpected ':', expecting tASSOC
     "format":'json',

I was wondering why this was? Does ruby not like hashes with “:” ? The reason I ask is that on foo_3 I have a json file I would like to put in that is formatted like:

     [{"id":"abc123","first":1.8, "second": 2.8},
     {"id":"abc456","first":-1.5, "second": 1.2}]

So when I try and use it also get the:

new.rb:10: syntax error, unexpected ':', expecting tASSOC

There are around 2000 id’s – so I cant change all : to => manually and this will be dynamic as well. So I am a little stuck!

SO either I have to find a way to change all “:” to “=>” before I send the array or, I am doing something stupid and very wrong.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:11:40+00:00Added an answer on May 31, 2026 at 7:11 am

    This is the new hash syntax from Ruby 1.9. These two forms are identical

    {foo: 1, bar: 2}
    {:foo => 1, :bar => 2}
    

    After formatting to JSON, symbols become strings, so

    {foo: 1, bar: 2}.to_json
    {:foo => 1, :bar => 2}.to_json
    {"foo" => 1, "bar" => 2}.to_json
    

    all produce the same output.

    Summary: don’t bother changing your hashes to the new syntax. It’s working just fine.

    Update

    I just re-read your question and noticed that you mention a “JSON file” that you want to insert into ruby hash. I don’t know what code you use for this, but it’s not gonna fly. JSON spec requires quoted key names, and Ruby hash syntaxes (both of them) are not JSON-compatible. So you can’t just take some JSON and pretend that it’s a Ruby hash. You can parse it, though.

    require 'json'
    
    json_string = "{\"id\":\"abc123\",\"first\":1.8, \"second\": 2.8}"
    ruby_hash = JSON.parse json_string
    # {"id"=>"abc123", "first"=>1.8, "second"=>2.8}
    

    Update 2

    Since ruby 2.2, there is a third variant of hash syntax which IS json compatible. So you can take a json string and simple eval it.

    json_string = '{"id":"abc123","first":1.8, "second": 2.8}'
    eval(json_string) # => {:id=>"abc123", :first=>1.8, :second=>2.8}
    

    Don’t eval it, though. If it’s a JSON string, use JSON.parse on it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to query Sugar through its REST API using Java for entries in
I am trying to query the windows desktop search API using SQL. I have
I'm trying to access a 3rd party api over http using jquery .ajax (or
I am trying to send this query: http://api.geonames.org/search?featureCode=PRK&maxRows=10&username=demo&country=US&style=full&adminCode1=AK To a web service and pull
Using JPA 2 with EclipseLink implementation. I'm trying to build a dynamic query which
I'm trying to use Google Analytics API to query internal searches that happen on
I'm trying to fetch the XML data from a query to the api without
I'm trying to create a query with the Criteria API from JPA 2.0, but
I'm trying to query a MySQL database using an array but I'm having trouble!
I'm trying to query using Castle and return a strong-typed array or IList for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.