How do I delete multiple portions of a JSON file that contain specific text in C#

Viewed 49

I have 10,000 JSON files which contain attributes, some of which are marked as Empty.

Attributes that must stay look like this: {"trait_type":"Skin","value":"Euphoria"}, whilst attributes that must be removed look like this {"trait_type":"Tattoo","value":"Empty"},

These empty attributes appear multiple times in some JSON files, while sometimes none in others.

Here is my code, however I must apologise is it seems that i've barely tried, but I've deleted most of it now out of frustration.

using System.Web;
using System.Text.Encodings.Web;

internal class Program
{
    private static void Main(string[] args)
   
{
     var jsonPath = @"/Users/jasonnienaber/Desktop/nft/cef/official/fatJSONselfFIX/test/test2";
     var jsonFiles = Directory.GetFiles(jsonPath, "*.json").OrderBy(f => f);

     string toRemove = "Empty";

     foreach(var jsonFile in jsonFiles)
     {
        var json = File.ReadAllText(jsonFile);
        var sample = JsonSerializer.Deserialize<Sample>(json);
    
        if (json.Contains(toRemove))
        {

        }
     }
}
 public class Attribute
        {
            public string trait_type { get; set; } 
            public string value { get; set; }
    
        }
    
        public class Sample
        {
            public string image { get; set; }
            public string name { get; set; }
            public string description { get; set; }
            public string external_url { get; set; }
            public List<Attribute> attributes { get; set; }
            public string compiler { get; set; }
        }

}

I'm also aware that I have not re-sereliazed yet, and i'll need to delete the old file and write to a new file. I'm new to C# and just need assistance deleting the aforementioned Empty attributes.

1 Answers

I did it with .Net 6 and install Newtonsoft package. I did only the part that removes the wanted records from JSON. From here you can go and iterate the files and do your updates.

using Newtonsoft.Json.Linq;

internal partial class Program
{

static void Main(string[] args)
{
    string json = @"{""image"":""img"",
                    ""name"":""name"",
                    ""description"":""description"",
                    ""external_url"":""external_url"",
                    ""attributes"":[
                                    {""trait_type"":""Skin"",""value"":""Euphoria""},
                                    { ""trait_type"":""Tattoo"",""value"":""Empty""}
                                    ],
                    ""compiler"":""compiler""
                    }";
    
    dynamic jObj = JObject.Parse(json);
    
    var lst = (jObj.attributes as IEnumerable<object>).ToList();
        lst.RemoveAll(x => (x as dynamic).value == "Empty");

    dynamic outJosn = new
    {
        image = jObj.image,
        name = jObj.name,
        description = jObj.description,
        external_url= jObj.external_url,
        attributes = (dynamic)lst,
        compiler = jObj.compiler
    };
    
    string output = Newtonsoft.Json.JsonConvert.SerializeObject(outJosn);
}
}
Related