简体   繁体   中英

How to empty a JObject array in C#

I have the following json

{       
"audit_date": "2020-05-13T11:27:10.3187798Z",
"client_uuid": "2fd77dd8-ed76-4bba-b0e1-5cda454c8d6e",
"audit_entry": {
    "where_uri": "test.com/dataservice/apps/171f0841-825b-4964-8f8c-0869650f14a6",
    "why_uri": "test.com/dataservice/reference/reasons_for_change/61acc173-7168-4ae5-9f04- afa228941f8b",
    "who_uri": "test.com/securityservice/users/4977dae1-a307-425f-980c-53413fef1b0f",
    "when_audited": "2018-11-13T20:20:39+00:00",
    "what_uri": "test.com/dataservice/study_subjects/1bc67a71-8549-4ab8-9dd9-e44238198860",
    "what_changed": [
        {
            "attribute_name": "birth_year",
            "attribute_value": "1969",
            "attribute_change": null
        },
        {
            "attribute_name": "subject_reference",
            "attribute_value": "TEST-WOO3444",
            "attribute_change": null
        }
    ]
   }
}

But I want to empty the nest array "what_changed"

So I need the output to be

{       
"audit_date": "2020-05-13T11:27:10.3187798Z",
"client_uuid": "2fd77dd8-ed76-4bba-b0e1-5cda454c8d6e",
"audit_entry": {
    "where_uri": "test.com/dataservice/apps/171f0841-825b-4964-8f8c-0869650f14a6",
    "why_uri": "test.com/dataservice/reference/reasons_for_change/61acc173-7168-4ae5-9f04-afa228941f8b",
    "who_uri": "test.com/securityservice/users/4977dae1-a307-425f-980c-53413fef1b0f",
    "when_audited": "2018-11-13T20:20:39+00:00",
    "what_uri": "test.com/dataservice/study_subjects/1bc67a71-8549-4ab8-9dd9-e44238198860",
    "what_changed": []
  }
}

I have written the following code

        JObject jObj = JObject.Parse(jsonText);
        jObj["audit_entry"]["what_changed"] = null;
        string json = jObj.ToString(Formatting.None);

but this makes the field null rather than empty array.

I have also tried

        JObject jObj = JObject.Parse(jsonText);
        jObj["audit_entry"]["what_changed"] = "";
        string json = jObj.ToString(Formatting.None);

but that still doesn't give an empty array.

I also tried using the Array.Clear() method, but this is a JObject array rather than a normal array.

Arrays are represented by the JArray type, not JObject . Cast the value of "what_changed" to the proper type and use the methods you need. Eg:

JObject jObj = JObject.Parse(jsonText);
JArray changed=(JArray)(jObj["audit_entry"]["what_changed"]);
changed.Clear();

Working with JSON elements is rather unusual though. It's typically a lot easier to deserialize JSON strings into strongly typed objects, modify them as needed and then serialize them back to a string.

Generating the necessary DTOs can be done easily in Visual Studio by selecting Paste Special > Paste JSON as Classes from the Edit menu

I can see several possibilities... 1.- instead of clearing the array, create a new one, an empty one. This does not solve the problem but it is a work around. 2.- using newtonsoft (a nuget package that you can download), you may be able to find different utilities there. 2.1.- Instead of parsing with JObject, parse with JArray, and then use Clear: https://www.newtonsoft.com/json/help/html/ParseJsonArray.htm https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JArray_Clear.htm . 2.2.- use Remove, for this you need the property name, so you need to iterate within the array (a foreach), getting the name of the property, and delete one by one. https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JObject_Remove.htm

You need to cast it to a JArray first, then you can use its Clear() method;

((JArray)(jObj["audit_entry"]["what_changed"])).Clear();

Alternatively, you could simply create a new JArray in place of the old one.

jObj["audit_entry"]["what_changed"] = new JArray();

Try this :

JObject jObj = JObject.Parse(jsonText);
JToken jToken = jObj["audit_entry"]["what_changed"];
jToken.Replace(JToken.Parse("[]"));
string json = jObj.ToString(Formatting.None);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM