Search code examples
jsonbatch-processing

How to edit JSON files in batch?


So I have an NFT collection that I've created, it's containing 2 different designs in same collection. So I'd like to merge them together, but their JSON files are colliding. What I'm looking for is:

  • Increase a spesific number on spesific string.
  • Delete a line from them all completely.

It looks like this:

{
  "dna": "xxxxxxxxxxxxxxxxxxxx",
  "name": "XXXXX XXXXX #0", ##I'd like to change this number value. I'd like to add 3000 on top of every other one.
  "description": "xxxxxxxxxxxxxxxxxxxxxxxxxxx.",
  "image": "REPLACE-THIS-WITH-YOUR-URL/0.png", #How to replace in batch?
  "date": 1641544983764,
  "attributes": [
    {
      "trait_type": "xxxx",
      "value": "xxx"
    },
    {
      "trait_type": "xxx",
      "value": "xxx"
    },
    {
      "trait_type": "xxx",
      "value": "xxx"
    },
    {
      "trait_type": "xxx",
      "value": "xxx"
    },
    {
      "trait_type": "xxx",
      "value": "xxx"
    },
    {
      "trait_type": "xxx",
      "value": "xxx"
    },
    {
      "trait_type": "xxx",
      "value": "xxx"
    },
    {
      "trait_type": "xxx",
      "value": "xxx"
    },
    {
      "trait_type": "xxx",
      "value": "xxx"
    }
  ],
  "compiler": "xxxxxx" ##Delete this line from all
}

So there's 10.000 of these files, it's kinda impossible for me to manual edit. Any suggestions?


Solution

  • The basic code would be to (1) get a list of all the json files (2) read each file and parse into json object (3) create a new object and make the required changes (4) save the new file. As a side note I would backup all the files before running anything like this. Here is the basic code, you could work with someone who works with C# to implement this.

    private void button1_Click(object sender, EventArgs e)
    {
        //get all the json files
        var jsonPath = @"c:\temp\json";
        var jsonFiles = Directory.GetFiles(jsonPath, "*.json");
    
        //loop through each file and process according to specs
        foreach(var jsonFile in jsonFiles)
        {
            var json = File.ReadAllText(jsonFile);
            var sample = JsonConvert.DeserializeObject<Sample>(json);
            var sampleNew = new SampleNew();
    
            sampleNew.dna = sample.dna;
            sampleNew.name = sample.name + " 3000";
            sampleNew.image = sample.image.Replace("oldurl", "newurl");
            sampleNew.date = sample.date;
            sampleNew.attributes = sample.attributes;
    
            // serialize JSON to a string and then write string to a file
            File.Delete(jsonFile);
            File.WriteAllText(jsonFile, JsonConvert.SerializeObject(sampleNew));
        }
    
                
    }    
            public class Attribute
            {
                public string trait_type { get; set; }
                public string value { get; set; }
        
            }
        
            public class Sample
            {
                public string dna { get; set; }
                public string name { get; set; }
                public string description { get; set; }
                public string image { get; set; }
                public long date { get; set; }
                public List<Attribute> attributes { get; set; }
                public string compiler { get; set; }
            }
        
            public class SampleNew
            {
                public string dna { get; set; }
                public string name { get; set; }
                public string description { get; set; }
                public string image { get; set; }
                public long date { get; set; }
                public List<Attribute> attributes { get; set; }
            }
    

    Time to get your feet wet, or hire someone!