0

Suppose I have a JSON file that looks like the following:

    [
  {
    "CheckName": "test2",
    "Times": [
      "0001-01-01T00:00:00",
      "2019-02-27T09:29:39.5654213"
    ]
  },
  {
    "CheckName": "test",
    "Times": [
      "0001-01-01T00:00:00",
      "2019-02-27T09:29:39.574397"
    ]
  }
]

Which I am looping over as below:

string Config = File.ReadAllText(@"C:\Scripts\Config.json");
dynamic ConfigFile = JsonConvert.DeserializeObject(Config);

foreach (var item in ConfigFile) {
    string CurrentCheck = item.CheckName;
    DateTime LastRun = item.Times[0];
    DateTime NextRun = item.Times[1];
    //Do stuff with the read values here
}

I need to update the first time value of test2, and then, later on, update it on test. However, there may be 50+ items in this JSON file, and I don't know where I am in the file, other than via a foreach loop (I'm reading the JSON, then looping over it to READ the values I need, but I need to write changes). Is it possible to update each specific value as I come across it, and then rewrite the JSON back to disk while retaining the other values as they are?

Alternatively, do I perhaps need to change how my JSON looks in order to make this task easier.

4
  • Deserialise to define object? Is this really dynamic? Does it need to be? Deserialise to object modify item in the list, then serialise back. Commented Feb 27, 2019 at 9:36
  • This "Times" entry is an array of 2 value? It can be more? Always 2 as in your code you are accessing index 0 and 1? You only care about the 2 last value? I will go for a Custom Json Converter here. So Im will be sure that LastRun and NextRun are produce by the deserialisation. Commented Feb 27, 2019 at 9:44
  • @DragandDrop - There will only ever be 2 values under Times. Commented Feb 27, 2019 at 9:46
  • You may see a bonus in doing something like this: stackoverflow.com/questions/36757412/…. To automatically map Times array to 2 property LastRun and NextRun. In the current code of your question there is a small gain but if anyone start using the config (over program or dev), they will be glad to have a Custom for it. So there is only one place in the code where you have to care about index, and it's in the converter. Commented Feb 27, 2019 at 10:02

1 Answer 1

2

You've deserialised your JSON into an object, in your case called ConfigFile.

You can make changes to the ConfigFile, which is a dynamic object, and then re-serialise to get your JSON string again.

You might find it easier to deserialise your JSON into objects, rather than iterating over them using dynamic, which helps especially in handling nested objects without your code becoming very messy.

The fact you don't know where you are in the loop doesn't matter, because objects, including nested ones, are reference types. This means although you've created something called item, it is still just a reference to the item so modifying it in one place modifies it in all places. If that makes sense.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the quick asnwer. That certainly answers my question in full, I know where I need to go from here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.