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 8865349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:29:02+00:00 2026-06-14T16:29:02+00:00

Beginning rails programmer here. I’m trying to convert a hash to both xml and

  • 0

Beginning rails programmer here. I’m trying to convert a hash to both xml and json, but the output is different.

Here’s the hash:

{:exchangeRates=>[{:baseCurrency=>"USD", :quoteCurrency=>"EUR", :amount=>1, :nprices=>1, :conversions=>[{:date=>Tue, 20 Nov 2012 21:00:00 +0000, :ask=>"0.7813", :bid=>"0.7813"}]}, {:baseCurrency=>"CAD", :quoteCurrency=>"EUR", :amount=>1, :nprices=>1, :conversions=>[{:date=>Tue, 20 Nov 2012 21:00:00 +0000, :ask=>"0.7839", :bid=>"0.7837"}]}]}

Here’s the corresponding render code

format.json { render :json => { :response => rates.to_hash() } }

and here’s the JSON (which is what I want)

{"response": {"exchangeRates": [
    {
        "baseCurrency": "USD",
        "quoteCurrency": "EUR",
        "amount": 1,
        "nprices": 1,
        "conversions": [{
            "date": "2012-11-20T21:00:00+00:00",
            "ask": "0.7813",
            "bid": "0.7813"
        }]
    },
    {
        "baseCurrency": "CAD",
        "quoteCurrency": "EUR",
        "amount": 1,
        "nprices": 1,
        "conversions": [{
            "date": "2012-11-20T21:00:00+00:00",
            "ask": "0.7839",
            "bid": "0.7837"
        }]
    }
]}}

Here’s my xml render code:

format.xml { render :xml => rates.to_hash(), :root => 'response' }

Here’s the xml output (there are extra tags where I put in arrays):

<response>
    <exchangeRates type="array">
        <exchangeRate>
            <baseCurrency>USD</baseCurrency>
            <quoteCurrency>EUR</quoteCurrency>
            <amount type="integer">1</amount>
            <nprices type="integer">1</nprices>
            <conversions type="array">
                <conversion>
                    <date type="datetime">2012-11-20T21:00:00+00:00</date>
                    <ask>0.7813</ask>
                    <bid>0.7813</bid>
                </conversion>
            </conversions>
        </exchangeRate>
        <exchangeRate>
            <baseCurrency>CAD</baseCurrency>
            <quoteCurrency>EUR</quoteCurrency>
            <amount type="integer">1</amount>
            <nprices type="integer">1</nprices>
            <conversions type="array">
                <conversion>
                    <date type="datetime">2012-11-20T21:00:00+00:00</date>
                    <ask>0.7839</ask>
                    <bid>0.7837</bid>
                </conversion>
            </conversions>
        </exchangeRate>
    </exchangeRates>
</response>

As you can see, it is adding the extra “array” attribute tags, i.e. exchangeRates and conversions. How do I get this to format the same as the json? I also don’t want the attributes on any of the tags either. I know you can pass in attributes, such as :root => ‘response’, but after looking for quite some time, I can’t seem to find a listing of these attributes on the web.

Any help would be greatly appreciated, 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-06-14T16:29:04+00:00Added an answer on June 14, 2026 at 4:29 pm

    This is a case where it’s best just to go directly to the source code. The to_xml method is in the ActiveModel::Serializer module, here are the inline docs, which don’t mention anything about the type="array" attribute tags. Dig a bit deeper though and you’ll see they appear in the same file on line 130 of a method called add_associations.

    rails/activemodel/lib/active_model/serializers/xml.rb:130

    type = options[:skip_types] ? { } : {:type => "array"}
    

    That tells us that there’s an option called skip_types, which appears to be documented nowhere. Try passing that to to_xml, and you get the desired behaviour:

    a = [1, 2, 3]
    a.to_xml
    #=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<fixnums type=\"array\">\n  <fixnum type=\"integer\">1</fixnum>\n  <fixnum type=\"integer\">2</fixnum>\n  <fixnum type=\"integer\">3</fixnum>\n</fixnums>\n"
    a.to_xml(:skip_types => true)
    #=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<fixnums>\n  <fixnum>1</fixnum>\n  <fixnum>2</fixnum>\n  <fixnum>3</fixnum>\n</fixnums>\n"
    

    You’ll notice all the added type attributes are gone.

    So just pass the same option to render and you’ll get the desired result:

    format.xml { render :xml => rates.to_hash(), :root => 'response', :skip_types => true }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a beginning programmer in Ruby on Rails 3. I currently want to implement
This is probably really simple, but I'm still sorta beginning rails and can't seem
I am beginning to use Backbone and am trying to transition my Rails backend's
I'm comfortable with starting my own rails project but am trying to view and
I'm just beginning to (hopefully!) learn programming / ruby on rails and trying to
I'm currently reading Beginning Rails 3. I'm coming from PHP and trying to learn
Im teaching myself rails. Im a programmer but not a web programmer. Im going
I'm beginning development on a Rails app and before I start heading in the
I am just beginning to work with Rails. I have a model, User and
So I am beginning to work with Rails and I get some of the

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.