8 Replies
What do you mean?
1. Deserialize json
2. Add to the list/object you now have in memory
3. serialize json again
Depends on the shape of your "deserialized" json.
Is it an instance oof your class or something more abstract like a json node object?
the recommended approach to JSON in C# is to fully deserialize to a class that matches your schema, then manipulate the in memory instance of that class. This differs from languages like python or javascript where you usually just manipulate the "json tree" directly so to speak.
Deserialized json as in a C# object? Depends on your use case
You can add optional properties to a class, and the json will deserialize fine. Then just add data to the optional properties
Since your initial json did not have this data it makes sense it's optional
Second approach is to have a main object that is deserialized into, and then you clone this data into a new object containing the same properties, including the new relevant data
This can lead to code duplication, but depending on your use case this might be more valid if the new object is actually different
just to clarify, this approach is called "mapping" and is very common if you need structural changes to the data, instead of just "add one more item to an existing list"
i deserialized a json and i have device ids with channel ids inside and want to add a channel id to devices manually the json is from a rest api
Can you show a snippet of the json? And also the code you used to deserialize it