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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:07:55+00:00 2026-06-18T03:07:55+00:00

during run time I’d like to print JSon to log but censor one of

  • 0

during run time I’d like to print JSon to log but censor one of its fields.
I use JSon.Net and have all the attributes, for example

    public Class Terms
    {
        [JsonProperty("Term")]
        string term

        [JsonProperty("SecretTerm")]
        string SecretTerm

        public string toCensoredString()
        {
         // I need to get a JSON string with only the regular term and not the secret
         var jsonRequest = JsonConvert.SerializeObject(this);
         // .....
        }
    }

What will be the best way to eliminate specific field in runtime in my new ToCensoredString() function?

  • 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-18T03:07:56+00:00Added an answer on June 18, 2026 at 3:07 am

    By far the easiest approach is to use the JsonIgnore attribute.

    If I create a Terms object like this:

    Terms terms = new Terms() { SecretTerm = "Secret", Term = "Not secret" };
    

    And if SecretTerm looks like this:

    [JsonIgnore]
    [JsonProperty("SecretTerm")]
    public string SecretTerm { get; set; }
    

    Your serialized Json will look like this:

    {
        "Term": "Not secret"
    }
    

    If you want more fine-grained control you will have to create a custom converter.

    Edit:

    To more selectively output the object, you need the custom converter:

    class TermsConverter : Newtonsoft.Json.JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return typeof(Terms) == objectType;
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            Terms terms = (Terms)value;
    
            writer.WriteStartObject();
            writer.WritePropertyName("Term");
            writer.WriteValue(terms.Term);
            writer.WriteEndObject();
        }
    }
    

    When serializing, you would do this:

    var jsonRequest = JsonConvert.SerializeObject(this, new TermsConverter());
    

    You’ll note that I have left the ReadJson unimplemented – I don’t think it’s necessary as you can easily deserialize a Terms object without using a converter. In this case the SecretTerm property would simply be empty.

    By using the converter you won’t need the [JsonIgnore] attribute on the SecretTerm property.

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

Sidebar

Related Questions

I read somewhere that somebody could access a config value during run time but
How to remove disabled attribute from textbox in asp.net MVC during run time In
I have a constant value that never changes during run-time, but is impossible to
Everything compiles fine, but during run time, it crashes without any coredumps, exceptions or
I would like to have this info during run-time or in ipython. For example,
I'd like to instantiate an object of a generic class during run-time; I call
I would like to specify the regular expression during run-time, not compile-time. So that
I would like to conditionally change the background image of my Cluetips during run-time
I need to set the value of static float variable during run time but
I have an activity that could show different dialogs during run-time. I use onCreateDialog(int

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.