11

I have a JSON:

{
    "scbs_currentstatus": "",
      "scbs_primaryissue": "",
      "_umb_id": "Test",
      "_umb_creator": "Admin",
      "_umb_createdate": "0001-01-01 00:00:00",
      "_umb_updatedate": "0001-01-01 00:00:00",
      "_umb_doctype": "Test",
      "_umb_login": "Test",
      "_umb_email": "Test",
      "_umb_password": {
        "newPassword": "Test",
        "oldPassword": null,
        "reset": null,
        "answer": null
      },
      "_umb_membergroup": {
        " User": false,
        "Line User": true,
        "Callback User": false,
        "Su User": false,
        },
      "umbracoMemberComments": "Test",
      "umbracoMemberFailedPasswordAttempts": ""

    }

Iam trying to remove all the properties start with "umb_" .is this possible in json.net?

and output will be like:

{
        "scbs_currentstatus": "",
          "scbs_primaryissue": "",
           "umbracoMemberComments": "Test",
          "umbracoMemberFailedPasswordAttempts": ""
}

using remove i am able to to do it however not all at a time.

   result.Property("_umb_id").Remove();

any suggestion please?

3 Answers 3

29

You can parse the string first:

var temp =  JArray.Parse(json);
temp.Descendants()
    .OfType<JProperty>()
    .Where(attr => attr.Name.StartsWith("_umb_"))
    .ToList() // you should call ToList because you're about to changing the result, which is not possible if it is IEnumerable
    .ForEach(attr => attr.Remove()); // removing unwanted attributes
json = temp.ToString(); // backing result to json

UPDATE OR:

result.Properties()
    .Where(attr => attr.Name.StartsWith("_umb_"))
    .ToList()
    .ForEach(attr => attr.Remove());

UPDATE #2

You can specify more conditions in where clause:

.Where(attr => attr.Name.StartsWith("_umb_") && some_other_condition)

OR

.Where(attr => attr.Name.StartsWith("_umb_") || some_other_condition)

Or whatever you need.

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

1 Comment

@javed. can we specify more than one condition in it?suppose we dont the fields contains "umbracoMember" in the property.
0

In case JArray.Parse is not working, you can strongly type your object as follows:

var temp = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(json);
temp.Descendants()
 .OfType<JProperty>()
 .Where(attr => attr.Name.StartsWith("_umb_"))
 .ToList() // you should call ToList because you're about to changing the result, which is not possible if it is IEnumerable
 .ForEach(attr => attr.Remove()); // removing unwanted attributes

Comments

-3

I just ended up deserializing to JObject and recursively looping through that to remove unwanted fields. Here's the function for those interested.

private void removeFields(JToken token, string[] fields)
{
JContainer container = token as JContainer;
if (container == null) return;

List<JToken> removeList = new List<JToken>();
foreach (JToken el in container.Children())
{
    JProperty p = el as JProperty;
    string propertyName = p.hasOwnProperty(key);

    if (p != null && fields.Contains(p.propertyName) && p.propertyName.substring(0,4) == "_umb" )
    {
        removeList.Add(el);
    }
    removeFields(el, fields);
}

foreach (JToken el in removeList)
{
    el.Remove();
}
}

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.