2

I'm setting up audit log for web api. Here i would like to remove password property from json string because of security reason. I don't want to log login password for login API.

Json/Request Payload:

{"code":"medics1","username":"admin","password":"password"}

I tried with below code:

var payload = JArray.Parse(objQueue.payload);
payload.Remove("password");
3
  • Can you provide us what you have tried so far...? Any code or implementation Commented Apr 19, 2019 at 5:55
  • Please check updated question Commented Apr 19, 2019 at 5:58
  • @NayanRudani, I adde my answer below with output screenshot. Try it and let me know :) Commented Apr 20, 2019 at 11:34

3 Answers 3

3

You are closed to your goal. You need just one little change in your code.

You are using JArray and this is use for parsing json that start and end with [...] not for {...}

And your json is a JObject that means its start and end with {...}

So just use JObject instead of JArray and then it will works

string json = @"{ 'code':'medics1','username':'admin','password':'password'}";

JObject jObject = JObject.Parse(json);

jObject.Remove("password");

string outputJson = jObject.ToString();

Output: (From Debugger)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

You should use jobject parse here. Below code from newtonsoft site should be helpful

string json = @"{
    'channel': {
     'title': 'Star Wars',
     'link': 'http://www.starwars.com',
     'description': 'Star Wars blog.',
     'obsolete': 'Obsolete value',
     'item': []
    }
 }";

JObject rss = JObject.Parse(json);
JObject channel = (JObject)rss["channel"];
channel.Property("obsolete").Remove();

You do not have named object, hence you can directly call

rss.Property(“password”).Remove();

Another alternative aproach is to deserialize into a class which ignores password property and then serialize it again.

Hope this helps.

Comments

1

You were almost correct, just you have to deserialize into an object instead of an array based on your JSON string.

string str = @"{'code':'medics1','username':'admin','password':'password'}";
JObject j = JsonConvert.DeserializeObject<JObject>(str);
j.Remove("password");
Console.WriteLine(j.ToString(Formatting.Indented));

Outputting:

{
  "code": "medics1",
  "username": "admin"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.