Read the contents of the file and deserialize it to a collection in C#, add the new record then serialise to JSON and overwrite the existing file.
Here's the class:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Street { get; set; }
public string City { get; set; }
public int Zipcode { get; set; }
}
Here's some code to do what you need:
var path = @"path_to_json_file";
var json = File.ReadAllText(path);
var people = JsonConvert.DeserializeObject<List<Person>>(json);
people.Add(new Person{Id = 789, Name = "Mike", City = "ASP.NET", Street = "MVC Avenue", Zipcode = 12345});
json = JsonConvert.SerializeObject(people);
File.WriteAllText(path, json);
Member
54 Points
518 Posts
How to insert json file in c#
Oct 29, 2018 11:56 AM|klbaiju|LINK
Hi,
I want to insert a record in json file
following is my json file
How to do this
Regards
Baiju
All-Star
194009 Points
28027 Posts
Moderator
Re: How to insert json file in c#
Oct 29, 2018 12:10 PM|Mikesdotnetting|LINK
Read the contents of the file and deserialize it to a collection in C#, add the new record then serialise to JSON and overwrite the existing file.
Here's the class:
Here's some code to do what you need: