Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append data in a json file in C#

Tags:

json

c#

How would i keep appending data? I have this:

{
  "13232": [
    "2012952"
  ]
}

And i want to add an another object to it, example:

{
  "13232": [
    "2012952"
  ],
  "19213": [
    "2016086"
  ]
}

This is the code i use:

JArray array = new JArray();
array.Add(Itemid);

JObject o = new JObject();
o[Userid] = array;

string json = o.ToString(Formatting.Indented);
//i know this keeps appending text but how would i append it inside the { and }?
File.AppendAllText("E:/media.json", json);

I literally have no idea how to keep adding it, but maybe someone else has?

like image 883
datboi Avatar asked Apr 20 '17 20:04

datboi


People also ask

How do I append to a json file?

Steps for Appending to a JSON File In Python, appending JSON to a file consists of the following steps: Read the JSON in Python dict or list object. Append the JSON to dict (or list ) object by modifying it. Write the updated dict (or list ) object into the original file.

Which function is used to append data using JSON?

To append data to the JSON file we have to store the previous data to a variable. To get the data of our JSON file we will be using the file_get_contents() function. The file_get_contents() reads a file into a string.

How do I add a JSON object to a json file?

You need to get all the json objects from the json file first, parse it, then add new json array to it, finally save it back to the file. Show activity on this post. This is the method that appends new string to existing Json File and make it in proper format.

What is JSON object in C?

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc. JSON stands for JavaScript Object Notation. The format was specified by Douglas Crockford.


2 Answers

You won't be able to use file append operations to do this. File append operations can only add text to the end, they can't insert text at some point in the middle. This makes it impossible to use file-append to keep the JSON valid.

You have two choices that I can think of:

  1. Read the entire file into an object, add your object, and then rewrite the entire file (poor performance)
  2. Open the file read/write, parse through until you get to the closing curly brace, then write the remaining data, then write the close curly brace (not trivial)
like image 196
Tim Avatar answered Sep 28 '22 08:09

Tim


The safest approach is read-update-rewrite (applies to JSON and XML format as they don't support appending).

Next option if you can live with invalid JSON is to simply concatenate JSON fragments with code you have and than use SupportMultipleContent in JsonReader to read fragments Read Multiple Fragments With JsonReader

If those approaches don't work and your format is fixed - find position of last ] in the file, seek stream there and write new array elements and append ]}.

like image 20
Alexei Levenkov Avatar answered Sep 28 '22 06:09

Alexei Levenkov