Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove specific properties from JSON object

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?

like image 327
Harshit Avatar asked Mar 29 '16 07:03

Harshit


People also ask

How do I remove a property from a JSON object?

Use the delete operator to remove a property from an object. let person = { firstName: "John", lastName: "Doe", gender: "Male", age: 34 }; // Delete the age property first delete person. age; let json = JSON. stringify(person); console.

How do I remove a specific element from a JSONArray?

You can remove an element from the JSONArray object using the remove() method. This method accepts an integer and removes the element in that particular index.

How do you remove the property name from this object?

The delete operator is used to remove these keys, more commonly known as object properties, one at a time. The delete operator does not directly free memory, and it differs from simply assigning the value of null or undefined to a property, in that the property itself is removed from the object.

How do I remove one key-value pair in JSON?

How do you remove a key value pair from a JSON object? JsonObject::remove() removes a key-value pair from the object pointed by the JsonObject . If the JsonObject is null, this function does nothing.


1 Answers

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.

like image 169
amiry jd Avatar answered Oct 05 '22 21:10

amiry jd