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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:33:20+00:00 2026-05-12T16:33:20+00:00

In rails you can do the following to convert an object to Json but

  • 0

In rails you can do the following to convert an object to Json but only with a subset of the fields included in the object.

@user.to_json :only => [ :name, :phone ]

Although i am using currently the ASP.NET MVC Json() function it doesn’t let me define which fields i want to include in the conversion. So my question is whether or not there is a function in JSON.NET or otherwise that will accept specific fields before doing the conversion to json.

Edit: Your answer should also cover the array scenario ie.

@users.to_json :only => [ :name, :phone ]
  • 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-12T16:33:21+00:00Added an answer on May 12, 2026 at 4:33 pm

    You could use anonymous types:

    public ActionResult SomeActionThatReturnsJson()
    {
        var someObjectThatContainsManyProperties = GetObjectFromSomeWhere();
        return Json(new {
            Name = someObjectThatContainsManyProperties.Name,
            Phone = someObjectThatContainsManyProperties.Phone,
        });
    }
    

    will return {"Name":"John","Phone":"123"}


    UPDATE:

    The same technique can be used for the array scenario:

    public ActionResult SomeActionThatReturnsJson()
    {
        var users = from element in GetObjectsFromSomeWhere()
                    select new {
                        Name = element.Name,
                        Phone = element.Phone,
                    };
        return Json(users.ToArray());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I create the following scenario that rails can provide for carrierwave, using
In Rails we can do the following in case a value doesn't exist to
in the rails source : https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb the following can be seen @load_hooks = Hash.new
I can run the following commands in the console for my Rails application and
I have the following query that I need to convert to rails like syntax.
In this Ruby on Rails helper , there is the following method: # Convert
Given following model: rails g model Menu name:string rails g model MenuHeader mh_name:string menu_id:integer
In a new Rails 3.2 app you can find in config/initializers/wrap_parameters.rb the following lines:
Can rails handle creating a view without a controller? For example, let say I
A single Rails command can make lots of changes in an app - creating

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.