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

  • Home
  • SEARCH
  • 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 3997750
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:26:24+00:00 2026-05-20T07:26:24+00:00

I am using Ruby on Rails 3 and I would like, in some way,

  • 0

I am using Ruby on Rails 3 and I would like, in some way, to convert a string to_json and to_xml.

Just to know, I need to return that string in a Rack method in this way:

[404, {"Content-type" => "application/json"}, ["Bad request"]]
# or
[404, {"Content-type" => "application/xml"}, ["Bad request"]]

However what I need is only to convert that string to_json and to_xml? How is it possible do that?

  • 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-20T07:26:25+00:00Added an answer on May 20, 2026 at 7:26 am

    Sometimes you must add a require 'json' in your file (after installing the gem, JSON implementation for Ruby ) and do:

    JSON.parse("Bad request").to_json   
    

    or you could try:

    ActiveSupport::JSON.encode("Bad request").to_json
    

    But in your case maybe the best approach is respond correctly:

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @somearray }
      format.json  { render :json => @somearray }
    end
    

    Alternatively you could do:

      mystring_json = '{"bar":"foo"}'
      [404, {'Content-Type' => 'application/json'}, [mysrting_json]] #json stuff
      mystring_xml = '<?xml><bar>foo</bar>'
      [404, {'Content-Type' => 'application/xml'}, [mysrting_xml]]  #xml stuff
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Ruby on Rails 3 and I would know in which case
I am running Ruby on Rails 3 and Prototype and I would like to
I am using Ruby on Rails and have a situation that I am wondering
I am using cucumber and RSpec for my new ruby on rails application and
I am learning Ruby On Rails. I am on a shared hosting with Ruby
I am learning Ruby on Rails, and I am very confused on how the
I am building a Rails site for a friend. It is mainly going to
I've been working on a small page in PHP, one that doesn't need the
I am having a hard time managing the storage and access of a large

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.