0

I'm trying to create a json file from c#. I need to remove this array, "judgmentLinkedAccount": [], if it's empty. I tried:

    public static void ReplaceText() 
    {
        string text = File.ReadAllText(FileToExport.exportFile);
        string textToReplace = "\"" + "judgmentLinkedAccount" + "\"" + ": []";
        text = text.Replace(textToReplace, "");
        File.WriteAllText(FileToExport.exportFile, text);
    }

But it doesn't remove the text. Thanks for any help.

Ben Wills
  • 11
  • 1
  • 1
    1) Can you share a [mcve] showing the input JSON? 2) Are you creating the JSON? If so, why not simply skip serializing the `judgmentLinkedAccount` member? E.g., if you are using [tag:json.net] to generate your JSON, see [Can Newtonsoft Json.NET skip serializing empty lists?](https://stackoverflow.com/q/11320968/3744182). – dbc Oct 29 '19 at 16:54

1 Answers1

0

change the content of textToReplace with textToReplace = "\"judgmentLinkedAccount\": []";

Marwen Jaffel
  • 703
  • 1
  • 6
  • 14