Manipulate Json

Anyone know how to add things to a deserialized json?
8 Replies
Buddy
Buddy5w ago
What do you mean?
Pobiega
Pobiega5w ago
1. Deserialize json 2. Add to the list/object you now have in memory 3. serialize json again
SleepWellPupper
Depends on the shape of your "deserialized" json. Is it an instance oof your class or something more abstract like a json node object?
Pobiega
Pobiega5w ago
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.
FusedQyou
FusedQyou5w ago
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
Pobiega
Pobiega5w ago
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"
Night-Shadow
Night-Shadow5w ago
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
Pobiega
Pobiega5w ago
Can you show a snippet of the json? And also the code you used to deserialize it
Want results from more Discord servers?
Add your server