C# Problem with serialize data to many json files

corwinus

New member
Joined
Jun 4, 2022
Messages
2
Programming Experience
Beginner
I have many json files with the same type data Examples:

File1.json:
{
"$id": "1",
"strings": [
    {
"Key": "some unique key1",
"Value": ”some value1”
    },
{
"Key": "some unique key2",
"Value": ”some value2”
    },
  ]
}


File2.json:
{
"$id": "1",
"strings": [
    {
"Key": "some unique key3",
"Value": ”some value3”
    },
{
"Key": "some unique key4",
"Value": ”some value4”
    },
  ]
}


I successfully merged and deserialized (used Newtonsoft.Json) data from all jsons to 2 files:
Keys.csv (content below, without header) Line_number;key;value;json_file_name:
1; "some unique key1”; ”some value1”;”File1.json”
2;  "some unique key2”; ”some value2”;”File1.json”
3;  "some unique key3”; ”some value3”;”File2.json”
4;  "some unique key4”; ”some value4”;”File2.json”


Values.txt :
”<1>some value1”
”<2>some value2”
”<3>some value3”
”<4>some value4”


After some changes in Values.txt I need to serialize/save it to json files (they have to be the same as source jsons – only values are changed).
So finally I need Files1.json and Files2.json with only values changed.
I am able to serialize to one json but have no idea how do it with many jsons.
 
Why did you merge them together in the first place? Is there some kind of inter dependence in the data that requires the data from separate files be merged together?

Anyway, it looks like you are keeping track of which file which piece of data came from. You can use LINQ extension methods GroupBy() and/or Where() to filter down to particular files. Then you can just serialize those that belong to a file.
 
I need values.txt in one file due to translation purposes (to import it to CAT tool). Additionally there are about 2000 source json files. Anyway I'll try your advices.
 
Ah! Our globalization team did something similar, though at that time they were dealing with input/output Windows Resources text files, but the translators/vendors wanted databases transmitted to them. What they did was have a database of resource IDs, strings, and language ID. The language ID was used to figure out which output file the string should go back to.
 
Back
Top Bottom