2

The easiest way to change column names in a .json file would be to stringify it and simply use replace. But that might affect values.

I could also parse it into a jsonobject and replace the key row by row, but this does not seem to be very efficient.

var json = client.DownloadString(item.uri);
dynamic holdJson= JsonConvert.DeserializeObject(json);

foreach(var item in holdJson) {
    item.setKey("replacement"); ?
}

Am I missing something here? What is the best way to replace or rename column headers in a json file?

edit: To ask my question more clearly: How do I change the Key in a JObject?

DGK
  • 2,717
  • 3
  • 29
  • 44
  • i hope below link will help you in javascript level same applies in C# with little change,please check this link http://stackoverflow.com/questions/13391579/how-to-rename-json-key eg. code var json = [{"_id":"5078c3a803ff4197dc81fbfb","email":"user1@gmail.com","image":"some_image_url","name":"Name 1"},{"_id":"5078c3a803ff4197dc81fbfc","email":"user2@gmail.com","image":"some_image_url","name":"Name 2"}]; json = JSON.parse(JSON.stringify(json).split('"_id":').join('"id":')); document.write(JSON.stringify(json)); – Chandru velan Apr 12 '17 at 10:14

1 Answers1

1

Although it might not be the correct answer it's possible to solve it like this:

var jsonFile = JsonConvert.DeserializeObject(json);

foreach (JObject row in jsonFile)
{
    row.Add("newKeyName", "value");
    row.Property("oldKey").Remove();
}

Just add and remove the row, the jobject gets updated and so does your jsonfile.

DGK
  • 2,717
  • 3
  • 29
  • 44