I'm currently dealing with a stream of json objects coming in to my application, and am having some difficulties figuring out what the best way of parsing them is. The stream consists of objects that have a defined type. The problem is that one of the fields in the object is of changing type. It looks like this:
[{
"status": "closed",
"type": "transaction",
"transaction": {
"TransactionType": "TypeA",
"Account": "Some string",
"Fee": "14",
"date": 45325680
},
"validated": true
},
{
"status": "closed",
"type": "transaction",
"transaction": {
"TransactionType" : "TypeB",
"Account" : "Some string",
"Fee": "42",
"Destination" : "Some string"
},
"validated": true
}]
You can see that the "parent" does not change, but the "transaction" does. I removed a lot of fields from the "transaction" field to make it easier to explain, but this is a type with 10-ish common fields and 10-ish changing fields that are dependent on the type. There are also 10 transaction types, this makes it pretty annoying to put everything into one struct and have a ton of optional fields. I was also thinking about one struct for every transaction type, but this does not work because then there is no way of specifying which type the field should have in the parent.
How would I parse these objects effectively? Unfortunately, I can't change the structure of the elements coming out of the stream. What would be the best way to solve this?